我異步執行多個請求,每個響應都返回一個xml。我需要提取xml(我使用TBXML解析它)並將其保存在Core Data中。這不能在主線程上完成,因爲UI會變得太慢。 我做每個XML響應如下:NSManagedObject的NSMergeConflict
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和插入在覈心數據的數據。 執行此操作時,出現如下所示的很多合併衝突。任何想法爲什麼或任何線索來調試這個問題?我沒有改變核心數據模型。
NSMergeConflict for NSManagedObject with objectID '...'
with oldVersion = 117 and newVersion = 118 and
old object snapshot = ... and new cached row = ...
你可以簡要描述一下你的traverseElement嗎? – edsko