檢視崩潰日誌,發現有幾條崩潰記錄是選相簿的崩潰,崩潰日誌如下:
#0 Thread
SIGABRT
0 libsystem_kernel.dylib __pthread_kill + 8
1 libsystem_pthread.dylib _pthread_kill$VARIANT$mp + 380
2 libsystem_c.dylib abort + 140
3 libsystem_malloc.dylib __malloc_put
4 libsystem_malloc.dylib _malloc_report + 64
5 libsystem_malloc.dylib free + 376
6 CoreFoundation 0x00000001df485000 + 13464
7 ********** __37-[HXPhotoManager fetchAlbums:albums:]_block_invoke.192 (HXPhotoManager.m:376)
8 CoreFoundation 0x00000001df485000 + 1028784
9 CoreFoundation 0x00000001df485000 + 1030320
10 libdispatch.dylib __dispatch_client_callout2 + 16
11 libdispatch.dylib __dispatch_apply_invoke_and_wait + 164
12 libdispatch.dylib _dispatch_apply_f$VARIANT$mp + 720
13 CoreFoundation 0x00000001df485000 + 1029888
14 Photos -[PHFetchResult enumerateObjectsWithOptions:usingBlock:] + 88
15 ********** -[HXPhotoManager fetchAlbums:albums:] (HXPhotoManager.m:0)
16 ********** -[HXDatePhotoViewController getAlbumList] (HXDatePhotoViewController.m:379)
17 ********** __40-[HXDatePhotoViewController viewDidLoad]_block_invoke_2 (HXDatePhotoViewController.m:117)
18 libdispatch.dylib __dispatch_call_block_and_release + 24
19 libdispatch.dylib __dispatch_client_callout + 16
20 libdispatch.dylib __dispatch_main_queue_callback_4CF$VARIANT$mp + 1068
21 CoreFoundation 0x00000001df485000 + 707812
22 CoreFoundation 0x00000001df485000 + 687020
23 CoreFoundation CFRunLoopRunSpecific + 436
24 GraphicsServices GSEventRunModal + 100
25 UIKitCore UIApplicationMain + 212
26 ********** main (main.m:17)
27 libdyld.dylib _start + 4
複製程式碼
檢視那個地方的程式碼,
// 是否按建立時間排序
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
if (self.type == HXPhotoManagerSelectedTypePhoto) {
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
}else if (self.type == HXPhotoManagerSelectedTypeVideo) {
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo];
}
// 獲取使用者相簿
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[userAlbums enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL * _Nonnull stop) {
// 獲取照片集合
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
// 過濾掉空相簿
if (result.count > 0) {
HXAlbumModel *albumModel = [[HXAlbumModel alloc] init];
albumModel.count = result.count;
albumModel.albumName = [HXPhotoTools transFormPhotoTitle:collection.localizedTitle];
albumModel.result = result;
[self.albums addObject:albumModel];
}
}];
複製程式碼
也都還正常沒有發現問題。 仔細查詢崩潰記錄,發現現有的記錄 崩潰的裝置都集中在 iOS 12.1.2 和12.1.4,部分12.0和12.0.1也有,而自己的 iOS12.1.4和 iOS12.1.2 還有 iOS12.0 都復現不了。難道又是 iOS 的坑麼?
後來又仔細看了下其他執行緒,發現還有一個執行緒也在讀取相簿:
#10 Thread
0 libsystem_kernel.dylib 0x00000001df12bea4 mach_msg_trap
1 libsystem_kernel.dylib 0x00000001df12b37c mach_msg
2 libdispatch.dylib 0x00000001def902d0 __dispatch_mach_send_and_wait_for_reply
3 libdispatch.dylib 0x00000001def90764 _dispatch_mach_send_with_result_and_wait_for_reply$VARIANT$mp
4 libxpc.dylib 0x00000001df1f3ea0 xpc_connection_send_message_with_reply_sync
5 Foundation 0x00000001e0166ab8 0x00000001dff1a000
6 Foundation 0x00000001dff2e56c 0x00000001dff1a000
7 Foundation 0x00000001dff3c334 0x00000001dff1a000
8 Foundation 0x00000001e016ca64 0x00000001dff1a000
9 CoreData 0x00000001e20de474 -[NSXPCStoreConnection sendMessage:store:error:]
10 CoreData 0x00000001e20de320 -[NSXPCStoreConnection sendMessageWithContext:]
11 CoreData 0x00000001e22c42b8 ___54-[NSXPCStoreConnectionManager sendMessageWithContext:]_block_invoke
12 libdispatch.dylib 0x00000001defda484 __dispatch_client_callout
13 libdispatch.dylib 0x00000001def87bd4 __dispatch_sync_invoke_and_complete
14 CoreData 0x00000001e20ddcac -[NSXPCStoreConnectionManager sendMessageWithContext:]
15 CoreData 0x00000001e20dd494 -[NSXPCStore sendMessage:fromContext:interrupts:error:]
16 CoreData 0x00000001e20d3940 -[NSXPCStore executeFetchRequest:withContext:error:]
17 CoreData 0x00000001e20d2b18 -[NSXPCStore executeRequest:withContext:error:]
18 CoreData 0x00000001e2229de4 ___65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke
19 CoreData 0x00000001e20bb254 -[NSPersistentStoreCoordinator _routeHeavyweightBlock:]
20 CoreData 0x00000001e20bbee4 -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
21 CoreData 0x00000001e20c447c -[NSManagedObjectContext executeFetchRequest:error:]
22 PhotoLibraryServices 0x00000001ed399224 -[PLManagedObjectContext executeFetchRequest:error:]
23 Photos 0x00000001ee3dc07c ___55+[PHFetchResult fetchObjectIDs:inManagedObjectContext:]_block_invoke
24 CoreData 0x00000001e21fae88 _developerSubmittedBlockToNSManagedObjectContextPerform
25 libdispatch.dylib 0x00000001defda484 __dispatch_client_callout
26 libdispatch.dylib 0x00000001def8799c __dispatch_sync_invoke_and_complete_recurse
27 libdispatch.dylib 0x00000001def874a0 __dispatch_sync_f_slow
28 CoreData 0x00000001e20c28b8 -[NSManagedObjectContext performBlockAndWait:]
29 Photos 0x00000001ee3dbca4 +[PHFetchResult fetchObjectIDs:inManagedObjectContext:]
30 Photos 0x00000001ee3dccfc ___57-[PHFetchResult fetchedObjectsUsingManagedObjectContext:]_block_invoke
31 AssetsLibraryServices 0x00000001ecf8378c ___pl_dispatch_sync_block_invoke
32 libdispatch.dylib 0x00000001defda484 __dispatch_client_callout
33 libdispatch.dylib 0x00000001def875c8 __dispatch_lane_barrier_sync_invoke_and_complete
34 AssetsLibraryServices 0x00000001ecf8375c pl_dispatch_sync
35 Photos 0x00000001ee3dcbf4 -[PHFetchResult fetchedObjectsUsingManagedObjectContext:]
36 Photos 0x00000001ee3da4c0 -[PHFetchResult initWithQuery:oids:registerIfNeeded:usingManagedObjectContext:]
37 Photos 0x00000001ee3da5fc -[PHFetchResult initWithQuery:]
38 Photos 0x00000001ee384e30 -[PHQuery executeQuery]
39 Photos 0x00000001ee323c3c +[PHAsset fetchAssetsInAssetCollection:options:]
40 ********** 0x0000000102657198 __37-[HXPhotoManager fetchAlbums:albums:]_block_invoke.192 (HXPhotoManager.m:0)
41 CoreFoundation 0x00000001df5802b0 0x00000001df485000
42 CoreFoundation 0x00000001df5808b0 0x00000001df485000
43 libdispatch.dylib 0x00000001defda4c4 __dispatch_client_callout2
44 libdispatch.dylib 0x00000001def8b7fc __dispatch_apply_invoke$VARIANT$mp
45 libdispatch.dylib 0x00000001defda484 __dispatch_client_callout
46 libdispatch.dylib 0x00000001def89b4c __dispatch_root_queue_drain
47 libdispatch.dylib 0x00000001def8a2c0 __dispatch_worker_thread2
48 libsystem_pthread.dylib 0x00000001df1bd17c _pthread_wqthread
複製程式碼
查了一些資料,估計是因為多執行緒造成記憶體分配衝突的原因,於是給 PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
這個方法加鎖,然後問題就消失了。
程式碼如下:
// 獲取使用者相簿
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
[userAlbums enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL * _Nonnull stop) {
// 獲取照片集合
// 在 iOS 12.0 12.1 12.1.2 12.1.4 上,這個地方會報 malloc_error 錯誤,
// enumerateObjectsWithOptions 這個 block 會分2個執行緒回撥,估計是多執行緒造成記憶體衝突的原因,所以這裡加上一個執行緒鎖。
[self.fetchAlbumsLock lock];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
// 過濾掉空相簿
if (result.count > 0) {
HXAlbumModel *albumModel = [[HXAlbumModel alloc] init];
albumModel.count = result.count;
albumModel.albumName = [HXPhotoTools transFormPhotoTitle:collection.localizedTitle];
albumModel.result = result;
[self.albums addObject:albumModel];
}
[self.fetchAlbumsLock unlock];
}];
複製程式碼
這個問題出現的確實比較詭異,有些手機比較容易出現,有些就是出現不了。而PHFetchResult
的enumerateObjectsWithOptions
這個方法,蘋果的文件上面並沒有說明是多執行緒回撥的。真是個大坑。