简体   繁体   中英

break iteration of photos ALAssetsLibrary

How do I break through the enumeration going on for ALAssetsLibrary enumerateAssets method with a boolean set. Can I just get out of the loop?

CODE:

[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    @try {
        if(group != nil) {
            @autoreleasepool {
                    int newNumberOfPhotos = [group numberOfAssets];
                    if (self.numberOfPhotosInSavedPhotos < newNumberOfPhotos) {
                        //only new photos

                        NSRange range = NSMakeRange(self.numberOfPhotosInSavedPhotos, newNumberOfPhotos-self.numberOfPhotosInSavedPhotos);
                        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
                        [group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                            @autoreleasepool {
                               if(someCondition)  {
 //get out of the enumeration block (that is, exit the method) or go to complete block
                                }

                                NSString *assetType = [result valueForProperty:ALAssetPropertyType];
                            }
                        } ];
               }         
            }
        } else {
            //enumeration ended

        }

    }
    @catch (NSException *e) {
        NSLog(@"exception streaming: %@", [e description]);
    }
}failureBlock:^(NSError *error){
    NSLog(@"Error retrieving albums stream: %@", [error description]);
    if (error.code==-3312  || error.code==-3311) {
    }
}];

To stop the assets enumeration, just set *stop = YES in the enumeration block.

If you want to stop both the outer and the inner enumeration, use different names for the stop variable and set both to YES :

[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *outerStop) {
    ...
    [group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *innerStop) {
         if (someCondition) {
             *innerStop = YES;
             *outerStop = YES;
         } else {
             // process asset
         }
     }
 }

Remarks: The @try/@catch block should normally not be necessary if you don't have a programming error inside your loops.

Your check for "new photos" looks suspicious, because the number of assets in each group is compared with the same number self.numberOfPhotosInSavedPhotos , perhaps you should check that part again.

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