我正在做2核心數據提取,當他們在大約同一時間執行時,他們將導致主線程死鎖,導致我的應用程序凍結。核心數據死鎖
提取是在主線程中進行的。處理這種情況的一種方法是確保提取不是在同一時間進行的,但我無法控制提取何時完成。有沒有其他辦法可以避免這種僵局?
堆棧追蹤暫停應用程序時是:
#0 0x9a5d191a in __psynch_mutexwait()
#1 0x9166713b in pthread_mutex_lock()
#2 0x01e5d591 in -[_PFLock lock]()
#3 0x01e5d56a in -[NSPersistentStoreCoordinator lock]()
#4 0x01e720ee in -[NSPersistentStoreCoordinator executeRequest:withContext:error:]()
#5 0x01e70539 in -[NSManagedObjectContext executeFetchRequest:error:]()
#6 0x0007cd62 in -[CoreDataHelper fetchEntity:predicate:andSortDescriptors:inManagedObjectContext:] at /xxx/CoreDataHelper.m:150
#7 0x000075f6 in -[RemindersListViewController refreshReminders] at /xxx/RemindersListViewController.m:64
#8 0x000082f8 in -[RemindersListViewController refreshGUI] at /xxx/RemindersListViewController.m:187
#9 0x00003735 in __58-[AppDelegate setTabCountAndScheduleRemindersInBackground]_block_invoke_2 at /xxx/AppDelegate.m:114
#10 0x022dc53f in _dispatch_call_block_and_release()
#11 0x022ee014 in _dispatch_client_callout()
#12 0x022de7d5 in _dispatch_main_queue_callback_4CF()
#13 0x0261aaf5 in __CFRunLoopRun()
#14 0x02619f44 in CFRunLoopRunSpecific()
#15 0x02619e1b in CFRunLoopRunInMode()
#16 0x02fd77e3 in GSEventRunModal()
#17 0x02fd7668 in GSEventRun()
#18 0x00d9dffc in UIApplicationMain()
#19 0x0000297d in main at /xxx/main.m:16
編輯:訪問核心數據的不同部分正在嘗試做一個提取請求。它們都調用這個方法:
NSArray *reminders = [[CoreDataHelper sharedInstance] fetchEntity:APReminderEntity predicate:nil andSortDescriptors:[NSArray arrayWithObject:dateAscendingDescriptor] inManagedObjectContext:nil];
CoreDataHelper.m
- (NSArray *)fetchEntity:(NSString *)entity predicate:(NSPredicate *)predicate andSortDescriptors:(NSArray *)sortDescriptors inManagedObjectContext:(NSManagedObjectContext *)context {
DLogName()
if (context == nil) {
// Use default MOC
context = self.managedObjectContext;
}
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
if (predicate != nil) {
[request setPredicate:predicate];
}
if (sortDescriptors != nil) {
[request setSortDescriptors:sortDescriptors];
}
NSError *error = nil;
NSArray *entities = [context executeFetchRequest:request error:&error];
if (entities == nil) {
DLog(@"There was an error fetching entity: %@ Error: %@", entity, [error userInfo]);
entities = [NSArray array];
}
return entities;
}
編輯2:我只是試圖把我去取@synchronized
塊內請求(如本post建議)和到目前爲止,它似乎工作得很好......我正在訪問我的MOC正確的線程上。MOC初始化爲_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
? – 2013-03-11 10:02:58
@RajanBalana沒有理由......會在後臺線程中運行提取幫助嗎?它們不會死鎖麼? – 2013-03-11 10:16:17
「使用後臺線程」不是一種神奇的修復方法。在給出Core Data使用情況下,BG線程可能是合適的,但我們可以我們不知道目前所知道的是什麼 – occulus 2013-03-11 11:09:13