我花了數天的時間嘗試解決此問題的所有可能的解決方案,但似乎沒有任何解決方法。在覈心數據中使用多個上下文時出現錯誤133000
我運行一個後臺線程這樣的:
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
Media *localMedia = [media inContext:localContext];
UIImage *image = localMedia.thumbnail;
dispatch_async(dispatch_get_main_queue(), ^{
[thumbnails setObject:image forKey:[NSNumber numberWithInt:i]];
[contentDict setObject:thumbnails forKey:@"MediaItems"];
[cell.entryView setNeedsDisplay];
});
}];
或者這樣:
dispatch_queue_t cellSetupQueue = dispatch_queue_create("com.Journalized.SetupTimelineThumbnails", NULL);
dispatch_async(cellSetupQueue, ^{
NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator = [NSManagedObjectContext contextForCurrentThread].persistentStoreCoordinator;
[newMoc setPersistentStoreCoordinator:coordinator];
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:newMoc];
Media *localMedia = [media inContext:[NSManagedObjectContext contextForCurrentThread];
UIImage *image = localMedia.thumbnail;
dispatch_async(dispatch_get_main_queue(), ^{
[thumbnails setObject:image forKey:[NSNumber numberWithInt:i]];
[contentDict setObject:thumbnails forKey:@"MediaItems"];
[cell.entryView setNeedsDisplay];
});
}];
這兩個給我一個崩潰與返回的UIImage如無物,和Cocoa Error 133000
。
我已經刪除了所有其他背景線程代碼,並且直接保存在主線程中,以確保它的正確性。在主線程上運行上面的代碼也可以,但凍結了我的用戶界面。儘管做了所有這些努力,上面的代碼每次都會崩潰。
我不確定該做什麼該怎麼做。
編輯:
我的問題,具體是怎麼做的我做這項工作沒有崩潰?這似乎是來自另一個上下文中不存在的對象的問題,但是如何讓它們存在於另一個上下文中?
你的問題是?你正在使用RestKit?而且你的代碼片段非常不完整 - 我無法追蹤任何內容。觀察者方法幾乎肯定不是你想要的,第一個例子可能因saveInBackgroundWithBlock內部的東西而崩潰: – 2012-08-03 13:37:43
這可能是因爲核心數據不是線程安全的。你嘗試過[localContext performBlock:^ {}]; – 2012-08-03 14:42:35
@PauldeLange我沒有使用RestKit,沒有。我應該以觀察者方法取得什麼? – Andrew 2012-08-03 15:13:22