2011-01-27 68 views
8

我正在編寫一個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]; 
} 

回答

17

This interesting part is that this error only occurs the first time I launch the app after installing it. All subsequent launches after it's installed work fine. Does anyone know why this error is happening and why it would only happen on initial install.

我的猜測則是,持久性存儲是不正確關聯在第一次啓動時磁盤上的文件正確。當您將其URL分配給持久性存儲協調器時,支持Core Data存儲的文件不會實現。它只在第一次保存時才實現。

並且沒有準備備份文件的情況下合併更改會導致很多問題。

在創建後臺線程之前,在Core Data上下文仍爲空時,嘗試在執行初期首次啓動時從主線程保存上下文一次。希望這會解決你的問題。

+0

是的,這是有效的,我添加了代碼,在應用程序啓動之前刪除所有對象,之後發生任何其他背景事件,並且它做了訣竅。這是正常的嗎?我的設計有什麼問題嗎?我目前已經在應用程序中啓動了一個同步操作隊列:didFinishLaunchingWithOptions,用於下載數據和插入。 – marchinram 2011-01-27 17:29:58

0

您是否有任何其他mergeChangesFromContextDidSaveNotification通知被任何其他上下文所遵守?如果是這樣,那可能是您要通知的訂單。它可能會通知一個上下文,該上下文不知道這些更改會影響到的模式(也就是說,'Object的持久性存儲無法從此NSManagedObjectContext的協調程序中訪問')。

相關問題