Search code examples
iosios6alassetslibrary

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


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.


Solution

  • 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!