0
我遇到了與我的核心數據實現和線程相關的問題。它在iOS 7上正常工作,但無法在iOS 6上正常刷新主環境。CoreData線程。 iOS 6 vs 7
我有兩個NSPersistentStoreCoordinators指向同一個數據庫文件。然後我有一個主要背景和背景背景。所有新的子上下文都是後臺上下文的子項。
當我更新6上後臺線程中子上下文的項時,它們從不合併到主上下文,直到我重新啓動應用程序,並且主上下文從存儲中獲取它們。在ios7上,這一切都很好。我如何確保主要上下文正確刷新合併而不發生錯誤並重新加載數據庫?
我知道的iOS 7默認開啓 - 異步SQL訪問,但我已經做了上6:
NSSQLitePragmasOption : @{@"journal_mode" : @"WAL"}
這裏是我的上下文的是如何設置:
self.mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[self.mainManagedObjectContext setPersistentStoreCoordinator:self.mainPersistentStoreCoordinator];
[self.mainManagedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
[self.mainManagedObjectContext setUndoManager:nil];
self.backgroundParentContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[self.backgroundParentContext setPersistentStoreCoordinator:self.backgroundPersistentStoreCoordinator];
[self.backgroundParentContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
[self.backgroundParentContext setUndoManager:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:self.backgroundParentContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:self.mainManagedObjectContext];
這裏是我如何創建一個兒童背景:
- (NSManagedObjectContext *)newChildManagedObjectContext
{
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[childContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
[childContext setParentContext:self.backgroundParentContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedObjectContextDidSave:)
name:NSManagedObjectContextDidSaveNotification object:childContext];
[childContext setUndoManager:nil];
return childContext;
}
以下是通知方法:
- (void)mainContextDidSave:(NSNotification *)notification
{
__block NSNotification *strongNotification = notification;
[self.backgroundParentContext performBlockAndWait:^{
[self.backgroundParentContext mergeChangesFromContextDidSaveNotification:strongNotification];
}];
}
- (void)backgroundContextDidSave:(NSNotification *)notification
{
[self.mainManagedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:NO];
}
- (void)managedObjectContextDidSave:(NSNotification *)notification
{
[self.backgroundParentContext performBlockAndWait:^{
NSError *error = nil;
if (![self.backgroundParentContext save:&error]) {
DNSLog(@"error saving context: %@", error);
}
}];
}
UPDATE:
那麼看來這只是從來沒有打算在6上班據該文檔,他們已經改變了不少各地的合併上下文的7所以我不得不採取不同的方法爲6我猜...