Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 333
Jeremy Robin Avatar asked Nov 20 '12 19:11

Jeremy Robin


2 Answers

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!

like image 66
Jeremy Robin Avatar answered Nov 14 '22 20:11

Jeremy Robin


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.

like image 34
Feng Avatar answered Nov 14 '22 20:11

Feng