p0n*_*0ny 1 protocols objective-c ios
我正在编写一个简单的天气应用程序,试图巩固我对协议和代表的理解.我遇到以下情况:
最好的方法是什么?我正在考虑嵌套委托方法,其中数据源1在有数据(通过协议)时通知数据源2,然后让数据源2通知视图控制器在有数据时更新视图.但是,我认为这不是正确/最好的做事方式.
有任何想法吗?
谢谢
小智 5
这听起来非常适合GCD调度组.
首先,创建一个调度组dispatch_group_create.然后dispatch_group_enter在开始每个服务的请求之前使用.dispatch_group_leave当您收到每项服务的回复时,请致电.您可以指定在两个请求完成时要执行的操作dispatch_group_notify.
代码可能如下所示:
dispatch_group_t weatherGroup = dispatch_group_create();
dispatch_group_enter(weatherGroup);
[weatherService1 requestWithCompletion:^(Response *response){
// Do something with the response
dispatch_group_leave(weatherGroup);
}];
dispatch_group_enter(weatherGroup);
[weatherService2 requestWithCompletion:^(Response *response){
// Do something with the response
dispatch_group_leave(weatherGroup);
}];
dispatch_group_notify(weatherGroup, dispatch_get_main_queue(),^{
// This is executed after both requests are finished
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅文档:https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/#//apple_ref/doc/uid/TP40008079-CH2-SW19
| 归档时间: |
|
| 查看次数: |
925 次 |
| 最近记录: |