找出我的异步调用何时完成

时间:2011-12-12 13:13:41

标签: ios xcode asynchronous alassetslibrary

我遍历iOS设备上的相册列表。在迭代完这个小组后,我想简单地打印出找到的专辑数量。

在我的代码中,我必须更改NSLog语句仅在所有相册都已加载时执行。

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

void (^groupBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){
    if (group == nil){return;}
    [tempArray addObject:group];
 };

void (^failureBlock)(NSError *) = ^(NSError *error) {
    NSLog(@"A problem occured %@", [error description]);                                     
};  

[library enumerateGroupsWithTypes:ALAssetsGroupAll
                       usingBlock:groupBlock 
                     failureBlock:failureBlock];   

NSLog(@"%i albums were loaded", tempArray.count);

1 个答案:

答案 0 :(得分:5)

点名完成后,您的groupBlock会收到一组nil,因此请更改:

if (group == nil){return;}

NSLog(@"%i albums were loaded", tempArray.count);

来自班级参考:

  

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

[source]