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

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

枚举完成后,您的groupBlock将收到一组nil ,因此更改:

 if (group == nil){return;} 

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

从Class引用:

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

[资源]