2016-08-02 38 views
0

我正在調查在我的應用程序的Crashlytics控制檯中報告的某種程度上頻繁的崩潰。libobjc.A.dylib t objc_msg在後臺線程中發生核心數據使用的崩潰

之一許多例子我有具有以下應聲線程堆棧跟蹤:

#11. Crashed: com.apple.root.default-qos 
0 libobjc.A.dylib    0x22f3fa86 objc_msgSend + 5 
1 Foundation      0x23ee3005 -[NSString caseInsensitiveCompare:] + 28 
2 Foundation      0x23ed10bd _NSCompareObject + 28 
3 Foundation      0x23ed109d _NSSortFunctionOne + 120 
4 CoreFoundation     0x2373e6a3 __CFSimpleMergeSort + 114 
5 CoreFoundation     0x2373e6c5 __CFSimpleMergeSort + 148 
6 CoreFoundation     0x2373e6d9 __CFSimpleMergeSort + 168 
7 CoreFoundation     0x2373e6c5 __CFSimpleMergeSort + 148 
8 CoreFoundation     0x2373e6d9 __CFSimpleMergeSort + 168 
9 CoreFoundation     0x2368ac35 CFSortIndexes + 404 
10 CoreFoundation     0x2368c241 CFMergeSortArray + 176 
11 Foundation      0x23ed0a9d _sortedObjectsUsingDescriptors + 456 
12 Foundation      0x23f9c9fb -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 510 
13 MyApp       0x6d431 __24-[MyApp refresh]_block_invoke (MyApp.m:247) 
14 CoreFoundation     0x23769499 __NSArrayEnumerate + 372 
15 CoreFoundation     0x236e6c3b -[NSArray enumerateObjectsWithOptions:usingBlock:] + 62 
16 MyApp       0x6d17d -[MyApp refresh] (MyApp.m:263) 
17 MyApp       0xa97eb __52-[MyAppRequest updateAfterNotification:]_block_invoke (MyAppRequest.m:1175) 
18 libdispatch.dylib    0x23307cbf _dispatch_call_block_and_release + 10 
19 libdispatch.dylib    0x233136a1 _dispatch_root_queue_drain + 1572 
20 libdispatch.dylib    0x2331307b _dispatch_worker_thread3 + 94 
21 libsystem_pthread.dylib  0x234a6e0d _pthread_wqthread + 1024 
22 libsystem_pthread.dylib  0x234a69fc start_wqthread + 8 

崩潰的其他實例發生在相同的應用程序代碼(在MyApp類的refresh方法),但在不同的部分CoreFoundation sortedArrayUsingDescriptors方法邏輯。例如,另一個崩潰例如堆棧跟蹤有:

0 libobjc.A.dylib    0x1823cdb90 objc_msgSend + 16 
1 CoreFoundation     0x182c42738 CFStringCompareWithOptionsAndLocale + 232 
2 Foundation      0x183644840 _NSCompareObject + 64 
3 CoreFoundation     0x182d150f4 __CFSimpleMergeSort + 196 
4 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
5 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
6 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
7 CoreFoundation     0x182d1513c __CFSimpleMergeSort + 268 
8 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
9 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
10 CoreFoundation     0x182d15124 __CFSimpleMergeSort + 244 
11 CoreFoundation     0x182c3b738 CFSortIndexes + 472 
12 CoreFoundation     0x182c3cf58 CFMergeSortArray + 220 
13 Foundation      0x1836440f8 _sortedObjectsUsingDescriptors + 564 
14 Foundation      0x183725120 -[NSSet(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 564 
15 MyApp       0x10006f264 __24-[MyApp refresh]_block_invoke (MyApp.m:247) 

在刷新應用程序代碼是:

​​

其中的getProducts是:

- (NSArray*)getProducts { 
    NSFetchRequest *productFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Products"]; 

    // Filter 
    NSMutableArray *productsPredicates = [NSMutableArray array]; 
    [productsPredicates addObject:[NSPredicate predicateWithFormat:@"life_uid == %@", req.lifeUid]]; 
    [productsPredicates addObject:[NSPredicate predicateWithFormat:@"hidden == %@", @NO]]; 
    [productFetchRequest setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:productsPredicates]]; 

    // Sort 
    NSSortDescriptor *sortProductsByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
    [productFetchRequest setSortDescriptors:@[sortProductsByName]]; 

    [productFetchRequest setReturnsObjectsAsFaults:NO]; 
    [productFetchRequest setRelationshipKeyPathsForPrefetching:@[@"subprods", @"subprods.special"]]; 

    NSManagedObjectContext *moc = [MyAppCoreDataController sharedController].mainManagedObjectContext; 
    NSError *error = nil; 
    NSArray *products = [moc executeFetchRequest:productFetchRequest error:&error]; 
    if (error) { 
     NSLog(@"Error fetching products %@", error); 
    } 
    return products; 
} 

,並刷新被稱爲像這樣:

- (void)updateAfterNotification:(NSNotification *)notification { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     [[MyApp instance] refresh]; 
    }); 
} 

回答

0

很可能是因爲您沒有正確處理Core Data併發。當您使用NSPrivateQueueConcurrencyTypeNSMainQueueConcurrencyType創建管理對象上下文時,您必須必須將所有核心數據訪問放入performBlockperformBlockAndWait的調用中。這包括提取以及訪問提取對象的屬性值。

您正在使用dispatch_async,這不是處理Core Data的併發性的正確方法。您應該切換到使用performBlockperformBlockAndWait

+0

謝謝湯姆,我會在這方面讀一下。我在Core Data方面沒有太多的經驗,而且絕對沒有寫出我正在看的應用程序邏輯,所以這對我來說是比較新的。我會在什麼時候發表評論(如果?)我對這個東西有一個總體的瞭解! – Zach