找出当我的异步调用完成 [英] Find out when my asynchronous call is finished

查看:161
本文介绍了找出当我的异步调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过相册的iOS设备上的列表进行迭代。通过这组已经反复之后,我想简单地打印出被发现专辑的数量。

我有什么在我的code改变了的NSLog 语句只执行,当所有的专辑被加载。

  ALAssetsLibrary *库= [[ALAssetsLibrary的alloc]初始化];
NSMutableArray里* tempArray = [[NSMutableArray里的alloc]初始化];无效(^ groupBlock)(ALAssetsGroup *,* BOOL)= ^(ALAssetsGroup *组,BOOL *停止){
    如果(集团==无){回报;}
    [tempArray ADDOBJECT:组];
 };无效(^ failureBlock)(NSError *)= ^(NSError *错误){
    的NSLog(@时发生问题%@,[错误描述]);
};[库enumerateGroupsWithTypes:ALAssetsGroupAll
                       usingBlock:groupBlock
                     failureBlock:failureBlock];的NSLog(@%我的专辑被装,tempArray.count);


解决方案

groupBlock 将获得一组当枚举完成的,所以改变:

 如果(集团==无){回报;}

 的NSLog(@%我的专辑被装,tempArray.count);


从类引用:


  

在枚举完成后,的 enumerationBlock 的调用与设置为组。


<一个href=\"http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html\">[source]

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

解决方案

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]

这篇关于找出当我的异步调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆