Exception thrown by ALAssetsLibrary

Originator:kbeitz
Number:rdar://18341884 Date Originated:2014
Status:Open Resolved:
Product:iOS Product Version:8.0
Classification:Crash Reproducible:Yes
 
An exception gets thrown by ALAssetsLibrary if more than 16 are created and used. Attempting to enumerate groups with types triggers the following exception:
2014-09-15 13:20:19.000 AssetsTest[3544:1631342] *** Assertion failure in __addContextToList_block_invoke(), /SourceCache/PhotoLibraryServices_Sim/MobileSlideShow-2369/Sources/PLManagedObjectContext.m:2752

Steps to Reproduce:
In a stock project template, add this method to the view controller (along with the mutable array ivar and asset library ivar) and call it from viewDidAppear:

- (void)runTest
{
    static int counter = 1;

    NSLog(@"attempt %i",counter);
    counter++;

    NSAssert(_library == nil, @"_libary should be nil");

    _library = [[ALAssetsLibrary alloc] init];

    // uncomment this line and the crash doesn't happen.
//    [_array addObject:_library];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"%@",_library);

        [_library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            NSLog(@"group: %@",group);
        } failureBlock:^(NSError *error) {
            NSLog(@"ERROR: %@",error);
        }];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _library = nil;

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self runTest];
            });
        });
    });
}

Expected Results:
No exception should be thrown. Strangely, if you keep the instances of ALAssetsLibrary around (uncomment the line in the code above to add them to an array, effectively 'leaking' the library instances), the crash does not happen.

Actual Results:
The AssetsLibrary framework throws an exception and the app crashes.

Version:
iOS 8 GM

Notes:
A sample project can be provided. I will attempt to attach it to this bug as a comment.

Configuration:
iPhone 5

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!