我正在編寫一個Ipad應用程序,它顯示文章並在隊列上單獨的NSOperation中下載新文章,並將它們插入到核心數據中。目前,我有一個單獨的操作上下文,在操作的主要方法中創建,並使用與主要上下文相同的協調器。我使用了相同的模式,這種模式在NSManagedObjectContextDidSaveNotification的操作中已經提出了很多監聽,然後在主線程上下文中調用mergeChangesFromContextDidSaveNotification。問題是我得到這個錯誤:核心數據後臺線程NSManagedObjectContext合併錯誤
2011-01-27 07:26:02.574 Zagazine[12298:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'
*** Call stack at first throw:
(
0 CoreFoundation 0x3284b987 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x31aca49d objc_exception_throw + 24
2 CoreData 0x3549d07b _PFRetainedObjectIDCore + 638
3 CoreData 0x3549cdfb - [NSManagedObjectContext(_NSInternalAdditions) _retainedObjectWithID:] + 14
4 CoreData 0x354bf85b -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] + 2170
5 CoreFoundation 0x327e9bbf -[NSObject(NSObject) performSelector:withObject:] + 22
6 Foundation 0x320fd795 __NSThreadPerformPerform + 268
7 CoreFoundation 0x328017dd __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
8 CoreFoundation 0x327d34fb __CFRunLoopDoSources0 + 194
9 CoreFoundation 0x327d2e5b __CFRunLoopRun + 230
10 CoreFoundation 0x327d2c87 CFRunLoopRunSpecific + 230
11 CoreFoundation 0x327d2b8f CFRunLoopRunInMode + 58
12 GraphicsServices 0x3094a4ab GSEventRunModal + 114
13 GraphicsServices 0x3094a557 GSEventRun + 62
14 UIKit 0x32c14329 -[UIApplication _run] + 412
15 UIKit 0x32c11e93 UIApplicationMain + 670
16 ArticleApp 0x0000233f main + 70
17 ArticleApp 0x000022f4 start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal: 「SIGABRT」.
這個有趣的部分是,這個錯誤只發生在我安裝它後第一次啓動應用程序。安裝後的所有後續啓動都可以正常工作。有誰知道爲什麼會發生這種錯誤,爲什麼只會在初次安裝時發生。
而且,這是怎麼了,我合併的情況下,當它接收通知,這就是所謂的後臺線程:
- (void)mergeChanges:(NSNotification *)notification {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
是的,這是有效的,我添加了代碼,在應用程序啓動之前刪除所有對象,之後發生任何其他背景事件,並且它做了訣竅。這是正常的嗎?我的設計有什麼問題嗎?我目前已經在應用程序中啓動了一個同步操作隊列:didFinishLaunchingWithOptions,用於下載數據和插入。 – marchinram 2011-01-27 17:29:58