使用异步块通过NSArray进行迭代

时间:2012-03-14 10:22:54

标签: objective-c ios cocoa-touch nsarray

不确定这是否是这个问题的正确标题..无论如何, 我有一个字典数组,我正在迭代字典,传入一个块,有时在将来执行(异步)并将结果保存在字典中。这是我的方法:

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {

    NSLog(@"%@", info);
    NSMutableDictionary *images = [NSMutableDictionary dictionaryWithCapacity:[info count]];

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    for (NSDictionary *dict in info) {
        NSURL *assetURL = [dict objectForKey:UIImagePickerControllerReferenceURL];

        [library assetForURL:assetURL
                 resultBlock:^(ALAsset *asset)  {
                     NSString *filename = asset.defaultRepresentation.filename;
                     UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
                     [images setObject:filename forKey:image];
                 }
                failureBlock:^(NSError *error) {
                    [SVProgressHUD dismissWithError:@"Error occured."];
                }
         ];
    }

    NSLog(@"%@", images);

    [library autorelease];
    [self dismissImagePickerVC];
}

当然,在这个方法结束时,图像是空的,但我的问题是,当每次迭代的所有块都被执行时,如何才能回调? Thansk

1 个答案:

答案 0 :(得分:0)

来自Apple's documentation

  

枚举完成后,调用enumerationBlock,并将group设置为nil。

枚举完成后,您的第一个区块会看到asset == nil