NSManagedObject 的 NSMergeConflict

发布于 2024-12-11 11:50:07 字数 1639 浏览 1 评论 0原文

我异步执行多个请求,每个响应返回一个 xml。我需要提取 xml(我使用 TBXML 来解析它)并将其保存在 Core Data 中。这不能在主线程上完成,因为 UI 会变得太慢。 我对每个 xml 响应执行以下操作:

dispatch_queue_t request_queue = dispatch_queue_create("com.queue.name", NULL);
dispatch_async(request_queue, ^{
      AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
      NSManagedObjectContext *newMOC = [[NSManagedObjectContext alloc] init];
      [newMOC setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];
      newMOC setUndoManager:nil];

      NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
      [notify addObserver:self 
            selector:@selector(mergeChanges:) 
            name:NSManagedObjectContextDidSaveNotification 
            object:newMOC];
      [self traverseElement:tbxml.rootXMLElement inMOC:newMOC];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:newMOC];
      [newMOC release];
});
dispatch_release(request_queue);

- (void)mergeChanges:(NSNotification*)notification 
{
    AppDelegate *theDelegate = [[UIApplication sharedApplication] delegate];
    [[theDelegate managedObjectContext] performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification 
    waitUntilDone:YES];

}

在 traverseElement 方法中,我解析 xml 并将数据插入到 Core Data 中。 执行此操作时,我遇到了很多合并冲突,如下所示。有什么想法或任何调试此问题的线索吗?我没有更改核心数据模型。

NSMergeConflict for NSManagedObject with objectID '...' 
with oldVersion = 117 and newVersion = 118 and 
old object snapshot = ... and new cached row =  ...

I execute multiple requests asynchronously and each response returns an xml. I need to extract the xml (I’m using TBXML to parse it) and save it in Core Data. This cannot be done on the main thread because the UI will get too slow.
I’m doing the following for each xml response:

dispatch_queue_t request_queue = dispatch_queue_create("com.queue.name", NULL);
dispatch_async(request_queue, ^{
      AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
      NSManagedObjectContext *newMOC = [[NSManagedObjectContext alloc] init];
      [newMOC setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];
      newMOC setUndoManager:nil];

      NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
      [notify addObserver:self 
            selector:@selector(mergeChanges:) 
            name:NSManagedObjectContextDidSaveNotification 
            object:newMOC];
      [self traverseElement:tbxml.rootXMLElement inMOC:newMOC];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:newMOC];
      [newMOC release];
});
dispatch_release(request_queue);

- (void)mergeChanges:(NSNotification*)notification 
{
    AppDelegate *theDelegate = [[UIApplication sharedApplication] delegate];
    [[theDelegate managedObjectContext] performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification 
    waitUntilDone:YES];

}

In the traverseElement method, I parse the xml and insert the data in Core Data.
I’m getting a lot of merge conflicts like the one below when executing this. Any ideas why or any clues to debug this issue? I haven’t changed the Core Data model.

NSMergeConflict for NSManagedObject with objectID '...' 
with oldVersion = 117 and newVersion = 118 and 
old object snapshot = ... and new cached row =  ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-12-18 11:50:07

合并冲突封装了尝试在托管对象上下文中保存更改时发生的冲突。

有两种情况可能会发生冲突:

托管对象上下文与其在持久存储协调器层的内存中缓存状态之间。在这种情况下,合并冲突具有源对象和缓存快照,但没有持久快照。

持久存储协调器和外部存储(文件、数据库等)处的缓存状态之间。在这种情况下,合并冲突有一个缓存快照和一个持久快照。

请参阅 文档在这里。

A merge conflict encapsulates conflicts that occur when attempting to save changes in a managed object context.

There are two situations in which a conflict may occur:

Between the managed object context and its in-memory cached state at the persistent store coordinator layer. In this case, the merge conflict has a source object and a cached snapshot but no persisted snapshot.

Between the cached state at the persistent store coordinator and the external store (file, database, etc.). In this case, the merge conflict has a cached snapshot and a persisted snapshot.

See the documentation here.

苦笑流年记忆 2024-12-18 11:50:07

这可能对您有用: http:// /www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/

本文解释了如何使用多线程来实现持久性。

This might be useful to you: http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/.

The article explains how you should multithread for persistency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文