等待多个街区完成 [英] Waiting for multiple blocks to finish

查看:71
本文介绍了等待多个街区完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些方法从互联网上检索一些对象信息:

I have those methods to retrieve some object information from the internet:

- (void)downloadAppInfo:(void(^)())success
                failure:(void(^)(NSError *error))failure;
- (void)getAvailableHosts:(void(^)())success
                  failure:(void(^)(NSError *error))failure;
- (void)getAvailableServices:(void(^)())success
                     failure:(void(^)(NSError *error))failure;
- (void)getAvailableActions:(void(^)())success
                    failure:(void(^)(NSError *error))failure;

下载的东西存储在对象属性中,这就是成功函数不返回任何内容的原因。

The downloaded stuff gets stored in object properties, so that is why the success functions return nothing.

现在,我想要一个这样的方法:

Now, I want to have one method like this:

- (void)syncEverything:(void(^)())success
               failure:(void(^)(NSError *error))failure;

除了调用上面的所有方法之外什么也没做,只有在每个方法执行完之后才返回成功或失败阻止。

Which does nothing else than calling all the methods above, and returning only after every single method has performed its success or failure block.

我该怎么做?

提示:我知道级联方法在彼此调用成功块会起作用。但是,当后来的实现包括更多方法时,这既不干净也不有用。

Hint: I am aware that cascading the methods calls in each others success block would work. But this is neither 'clean' nor helpful when later implementations include further methods.

尝试:

我试过在 NSOperation 中运行每个调用,并将 NSOperations 添加到 NSOperationQueue 后跟完成操作,它取决于前面的每一个操作。

I tried running each of the calls in an NSOperation and adding those NSOperations to an NSOperationQueue followed by a "completion operation" which depends on every one of the preceding operations.

这不起作用。由于即使在各自的成功/失败块返回之前,操作也被认为已完成。

This won't work. Since the operations are considered completed even before their respective success/failure blocks return.

我也尝试使用 dispatch_group 。但我不清楚我是以正确的方式做到这一点。不幸的是,它无法正常工作。

I also tried using dispatch_group. But it is not clear to me wether I am doing it the right way. Unfortunately, it is not working.

推荐答案

你几乎就在那里,问题很可能是那些方法是异步的,所以你需要一个额外的同步步骤。只需尝试以下修复:

You were almost there, the problem is most likely to be that those methods are asynchronous, so you need an extra synchronization step. Just try with the following fix:

for(Appliance *appliance in _mutAppliances) {
  dispatch_group_async(
     group,
     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       dispatch_semaphore_t sem = dispatch_semaphore_create( 0 );

       NSLog(@"Block START");

       [appliance downloadAppInfo:^{
          NSLog(@"Block SUCCESS");
            dispatch_semaphore_signal(sem);
       }
       failure:^(NSError *error){
         NSLog(@"Block FAILURE");
         dispatch_semaphore_signal(sem);
       }];

       dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

       NSLog(@"Block END");
 });

 dispatch_group_notify(
   group,
   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
     NSLog(@"FINAL block");
     success();
 });
}

这篇关于等待多个街区完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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