ios - 更改一个 NSManaged 对象会更改其他对象

标签 ios core-data

我在应用程序商店中有一个名为 Hapistats 的应用程序。它使用cloudkit。我正在将所有数据从 cloudkit 转换为 Coredata。

我有一个名为log的实体,它与activity具有一对多关系。

问题是当我尝试更新日志的事件时。让我们假设 log1 是一个日志,其中包含名为 activity1 的事件。当我尝试将 Activity1 添加到已存在的日志 log2 中时,log1 将不再拥有 Activity1,而 log2 将成功将 Activity1 保存到自身。

每次更新或创建新日志时都会发生这种情况。问题是显然只能有一个具有特定事件的日志。

// Edits the log
func editLog(log: Log, rating: Int, activitiesToAddToLog: [Activity]) {
    log.rating = Int16(rating)
    log.removeFromActivities(log.activities) // Empties the activities from the log
    let activitiesOrderedSet = NSOrderedSet(array: activitiesToAddToLog)
    log.addToActivities(activitiesOrderedSet) // Adds all the activites to the log
    saveToPersistentStore()
}

是唯一调用editLog()的地方

@IBAction func saveActivitiesButtonTapped(_ sender: Any) {
    // log is an optional because I use a landing pad for log.
    if let log = log {
        LogController.shared.editLog(log: log, rating: rating, activities: displayActivities[0])true)
    }
}

我希望我的代码会在调用 editLog() 时将事件添加到日志中,但会覆盖其他日志的事件。

最佳答案

我明白了。这是 Xcode 的问题。它实际上并不是一对多,所以我重新添加它并在通用设备上构建,它突然起作用了。

关于ios - 更改一个 NSManaged 对象会更改其他对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58665003/

相关文章:

ios - 在 TableView 部分中排序核心数据对象

swift - 如何从 subview 更新核心数据实体?

ios - UIScrollView 向上滚动

ios - 将 NSDictionary 中的特定元素添加到 NSArray

iphone - 加速 UIPageViewController

iphone - 如何使用sendsynchronousRequest :queue:completionHandler:

ios - 核心数据自动保存与手动保存

ios - swift 中单个对象的 NSNotification

ios - 核心数据拉动变化

ios - 核心数据 : "Cannot delete object that was never inserted."