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

发布于 2024-12-20 22:12:58 字数 761 浏览 1 评论 0原文

我遍历 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);

I iterate through a list of photo albums on a iOS device. After having iterated through this group I want to simply print out the number of albums that were found.

What do I have to change in my code that the NSLog statement is only executed, when all the albums were loaded.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2024-12-27 22:12:58

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

if (group == nil){return;}

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

From the Class引用:

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

[来源]

Your groupBlock will receive a group of nil when the enumeration is completed, so change:

if (group == nil){return;}

to

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

From the Class reference:

When the enumeration is done, enumerationBlock is invoked with group set to nil.

[source]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文