Has anyone experienced crashes when using ALAssetsLibrary in a background thread?

徘徊边缘 提交于 2019-12-21 04:13:44

问题


I have an ios app which has not crashed in this way on ios 5 which is now crashing consistently on ios 6 on startup after 4 or 5 bg/fg cycles. I've traced the issue to my invocations of ALAssetsLibrary enumerateGroupsWithTypes (the app syncs to the underlying photo library whenever it starts up). The calls to enumerateGroupsWithTypes are made from within a background thread invoked via the dispatch queue so that the sync code can finish even if the user sends the app to the bg before it finishes. The crash message I receive is always the same:

* Assertion failure in __addContextToList_block_invoke_0(), /SourceCache/PhotoLibraryServices/MobileSlideShow-1647.5/Sources/PLManagedObjectContext.m:1305

and

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Too many contexts. No space in contextList.'

Googling for these error messages hasn't yielded anything. Since this never happens until the app has cycled on/off at least 5 times, I'm thinking that maybe the blocks are not being correctly removed from apple data structures when they finish? Thanks in advance for any leads.

UPDATE: After more investigating, this appears related to syncing ALAssetsGroupLibrary. The crash does not occur when i only sync ALAssetsGroupSavedPhotos or if there are 0 photos in ALAssetsGroupLibrary. It will occur if I sync only ALAssetsGroupLibrary and there is at least 1 photo in there.


回答1:


It turns out this has all been related to reallocating the ALAssetsLibrary for each sync. By adding a member variable instead, the crashing appears to have disappeared.

assetsLibrary = [[ALAssetsLibrary alloc] init];

While this is clearly a more efficient/better design for my code, I'd say the problems I've had indicate some ARC issue with ALAssetsLibrary and threading. Make sure to only allocate once!




回答2:


I suffered same issue:

For short: While the ALAssetsLibrary instance is enumerating with types or the ALAssetsGroup instaces enumerated last step are enumerating assets, the ALAssetsLibrary instance and the ALAssetsGroup instances should never been changed before all the enumerating blocks are finished.




回答3:


ALAssetsLibrary enumeration runs in the main thread (see this SO answer). I suspect this is because the assets library may want to interact with the user for permissions to use location data (because photos have geotagging).

This may be the source of your problem, if your code assumes that ALAssetsLibrary will continue to run in a background thread.




回答4:


You can checkout this. I have this problem before.But fix it by creating a singleton ALAssetsLibrary object

https://stackoverflow.com/a/32693118/3103450



来源:https://stackoverflow.com/questions/13480611/has-anyone-experienced-crashes-when-using-alassetslibrary-in-a-background-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!