Question

I noticed that even though I set the *stop BOOL pointer to YES, my enumeration block is being executed twice. I thought setting *stop = YES would stop after the first one?

[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    NSLog(@"Enumerating a group");

    //should stop after first?
    *stop = YES; 
} failureBlock:^(NSError *error) {
}];

The logs:

2014-03-05 12:27:29:363 AppName[3625:1547] Enumerating a group
2014-03-05 12:27:29:379 AppName[3625:1547] Enumerating a group

From the documentation, -enumerateGroupsWithTypes:usingBlock: is asynchronous, but is it also automatically parallelized? Both enumerations happen on the main thread, so maybe I should enforce my own stop? Seems weird to offer stop if it's not exactly enforced in the way the documentation suggests.

EDIT

For what it's worth, I added my own enforcement and it works. But why do I need to do this?

    __block BOOL stopItForReal = NO;
    [lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"Enumerating a group");

        //does not stop enumeration
        *stop = YES;

        //actually stops enumeration. I win, runtime!
        if (stopItForReal)
            return;
        stopItForReal = YES;

    } failureBlock:^(NSError *error) {
    }];
Was it helpful?

Solution

When group enumeration is terminated, ALAssetsLibraryGroupsEnumerationResultsBlock is invoked with group = nil. So you can write something like:

if (group != nil) 
{
    // your code.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top