我想初始化一个模型,让模型做一些异步的东西,并提交一个新的viewcontroller一旦完成。但是,我如何等待两个异步方法完成,以及如何设置回调方法?等待两种异步方法完成
伪
在我StartViewController.m:
-(void)openArticle
{
article = [Article initWithObject:someObject];
article.callback = changeView;
}
-(void)changeView
{
[self presentViewController:someController];
}
在我ArticleModel.m:
-(void)initWithObject:someObject
{
[self loadImage]
[self geoCode]
}
-(void)loadImage
{
runAsyncMethod: success:^() // This one is actually a AFNetworking setImageWithURLRequest
}
-(void)geoCode
{
runAnotherAsyncMethod: success:^() // This one is actually a geocodeAddressString operation
}
通过引用self作为'__weak'变量来避免保留周期的积分 – jackslash
谢谢。但是,如果我的一个AsyncMethod失败,我该如何处理错误?导致dispatch_group_notify()在成功和失败时被调用。 – fabian