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.
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!
You can checkout this. I have this problem before.But fix it by creating a singleton ALAssetsLibrary object
https://stackoverflow.com/a/32693118/3103450
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.
I suffered same issue:
For short: While the
ALAssetsLibrary
instance is enumerating with types or theALAssetsGroup
instaces enumerated last step are enumerating assets, theALAssetsLibrary
instance and theALAssetsGroup
instances should never been changed before all the enumerating blocks are finished.