Has anyone experienced crashes when using ALAssets

2019-03-27 14:24发布

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.

4条回答
Luminary・发光体
2楼-- · 2019-03-27 15:07

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!

查看更多
叼着烟拽天下
3楼-- · 2019-03-27 15:07

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

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

查看更多
虎瘦雄心在
4楼-- · 2019-03-27 15:12

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.

查看更多
男人必须洒脱
5楼-- · 2019-03-27 15:22

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.

查看更多
登录 后发表回答