如何知道块何时完成枚举ALAssetsLibrary

时间:2014-10-01 01:56:26

标签: ios objective-c-blocks enumeration alassetslibrary

我枚举ALAssetsLibrary以了解我的照片库有哪些组,

如何知道块何时停止枚举?

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);

1 个答案:

答案 0 :(得分:2)

if (group == nil)声明后检查enumerateAssetsUsingBlock

见下文:

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);

另外,你应该考虑分解你的块以使事情更容易阅读。例如:

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");
                     }];