简体   繁体   中英

ALAssets groups enumeration: how to check for completion?

ALAssets use a separated thread to manage enumeration, i have to know when enumeration terminate.

The block prototype for group enumeration is:

typedef void (^ALAssetsLibraryGroupsEnumerationResultsBlock)(ALAssetsGroup *group, BOOL *stop) ;

How can i add a completion block?

I found a solution that is documented only in part.

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

void (^groupsEnumerator)(ALAssetsGroup *,BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){ 
    if (group != nil) {
        [group enumerateAssetsUsingBlock:assetsEnumerator];

    }else {
        NSLog(@"group enumeration terminated");
    }
}; 

The same solution is valid for assets enumeration (this is not documented -.- )

 void (^assetsEnumerator)(ALAsset *,NSUInteger,BOOL*) = ^(ALAsset *result, NSUInteger index, BOOL *stop){    
    if (result !=nil) {
       //do something with result asset

    }else {
        NSLog(@"Assets enumeration terminated");
    }
};

I'm using this:

        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

        if (result == nil) {
            return;
        }

        if (index + 1 == group.numberOfAssets) {
        //Do what you want. Im using delegate to notify my parent class about finish.
            [delegate didGroupEnumerated:group];
        }

    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM