ALAssets groups enumeration: how to check for completion?

前端 未结 2 1680
孤城傲影
孤城傲影 2021-01-12 15:00

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

The block prototype for group enumeration is :

typede

相关标签:
2条回答
  • 2021-01-12 15:22

    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];
            }
    
        }];
    
    0 讨论(0)
  • 2021-01-12 15:31

    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");
        }
    };
    
    0 讨论(0)
提交回复
热议问题