Search code examples
iosobjective-c-blocksenumerationalassetslibrary

how to know when block finished enumerating ALAssetsLibrary


Im enumerating the ALAssetsLibrary to know what groups my photo library have,

How can I know when the block stopped enumerating?

NSMutableArray *albumsArray = [NSMutableArray array];

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll
                       usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
                           if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
                               [albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
                           }
                           NSLog(@"tu arraya :: %@", albumsArray);
                           //tha pics?
                           [group enumerateAssetsUsingBlock:  ^(ALAsset *result, NSUInteger index, BOOL *stop){
                               // ---> ALAsset:
                               NSLog(@"     Stop? %@", (stop ? @"YES" : @"NO") );
                           }];
                       }
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failure");
                     }];
NSLog(@"tu arraya 1:: %@", albumsArray);

Solution

  • Check if (group == nil) after the enumerateAssetsUsingBlock statement.

    See below:

    NSMutableArray *albumsArray = [NSMutableArray array];
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock: ^(ALAssetsGroup *group, BOOL *stop){
                               if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
                                   [albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
                               }
                               NSLog(@"tu arraya :: %@", albumsArray);
                               //tha pics?
                               [group enumerateAssetsUsingBlock:  ^(ALAsset *result, NSUInteger index, BOOL *stop){
                                   // ---> ALAsset:
                                   NSLog(@"     Stop? %@", (stop ? @"YES" : @"NO") );
                               }];
    
                               // this is the end of enumeration
                               if (group == nil)
                               {
                                   NSLog(@"THE END!!!");
                               }
                           }
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];
    NSLog(@"tu arraya 1:: %@", albumsArray);
    

    Also, you should really think about breaking up your blocks to make things easier to read. For example:

    NSMutableArray *albumsArray = [NSMutableArray array];
    ALAssetsLibrary *library = [BMBAssetManager defaultAssetsLibrary];
    
    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) =
        ^(ALAsset *result, NSUInteger index, BOOL *stop)
        {
            // ---> ALAsset:
            NSLog(@"     Stop? %@", (stop ? @"YES" : @"NO") );
        };
    
    void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) =
        ^(ALAssetsGroup *group, BOOL *stop){
            if ([group valueForProperty:ALAssetsGroupPropertyName] != nil) {
                [albumsArray addObject:[group valueForProperty:ALAssetsGroupPropertyName]];
            }
            NSLog(@"tu arraya :: %@", albumsArray);
            //tha pics?
            [group enumerateAssetsUsingBlock:assetEnumerator];
    
            // this is the end of enumeration
            if (group == nil)
            {
                NSLog(@"THE END!!!");
            }
    
        };
    
    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];