在 NSBatchInsertRequest 中创建的 NSManagedObjects 之间建立多对多关系 代码完整的错误日志

在我的示例应用中,CDStory 具有一对一关系 source,将 one CDStory 映射到 one { {1}}。

此外,CDNewsSource 具有一对多关系 CDNewsSourceone story 映射到 one 或 more {{ 1}} 个实例。

自然,CDNewsSource 的倒数是 CDStory

当我创建一个 source 实例时,我想立即设置它应该映射到的 story。但是,当我在批量插入请求中执行此操作时,我收到有关不同上下文的错误,即使我添加 CDStory 的上下文看起来应该与 CDNewsSource 的上下文相匹配.*

代码

CDNewsSource

*从错误日志中,我确实看到执行上下文是 CDStory,这可能与 let context = container.newBackgroundContext() context.performAndWait { /* Thread 3: "Illegal attempt to establish a relationship 'source' between objects in different contexts... */ let total = items.count var i = 0 let batchInsert = NSBatchInsertRequest(entity: CDStory.entity(),managedObjectHandler: { managedObject in guard i < total else { return true } defer { i += 1 } let story = managedObject as! CDStory let item = items[i] story.title = item.title let source = CDNewsSource(context: context) managedStoryObject.source = source /* Establishing relationship here. */ return false }) try! context.execute(batchInsert) } NSSQLBatchInsertRequestContext 中使用的背景上下文不同。不清楚这是怎么回事,因为我在执行和创建批量插入请求时指定了上下文。

完整的错误日志

managedObjectHandler
tomtomtom3 回答:在 NSBatchInsertRequest 中创建的 NSManagedObjects 之间建立多对多关系 代码完整的错误日志

在 Apple 文档档案中,我找到了答案:

批量更新不能用于更改关系、删除实体或创建新实体。

来源:https://developer.apple.com/library/archive/featuredarticles/CoreData_Batch_Guide/BatchUpdates/BatchUpdates.html

本文链接:https://www.f2er.com/671051.html

大家都在问