2015-10-05 61 views
0

我需要按顺序执行一些操作。 有许多对象符合一个协议(有一个方法,即execute())以执行的顺序插入到一个数组中。执行一批操作,并在每个操作的中间有一个用户交互

一切看起来简单使用GCD的东西,如:

dispatch_queue_t serialQuele = dispatch_queue_create("myQueue", NULL); 

for (id <ExecutableProtocol> exec in executables) 
{ 
    dispatch_async(serialQuele, ^{ 
     [exec executeWithCompletionBlock:^(NSString *name, Status *state) { 
      [self.executeResults setObject:state forKey:name]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       if (self.delegate) 
        [self.delegate didExecuted: exec withResult:state userInfo:userInfo]; 
      }); 
     }]; 
    }); 
} 

理念看起来不错,但如果执行要求有UIAlertController用户一个问题......接下来的执行也无法显示其对话框,UIAlertController因为用户尚未回答该问题,并且前一个对话框仍在显示。

我该如何达到我的预期?

谢谢。

回答

0

我认为切换第一个dispatch_async和循环之间的顺序并将dispatch_async更改为dispatch_sync可以达到您想要的效果。我不确定我是否理解你想要你的代码是做什么的,所以我可能是错的。

dispatch_async(serialQuele, ^{ 
    for (id <ExecutableProtocol> exec in executables) 
    { 
     [exec executeWithCompletionBlock:^(NSString *name, Status *state) { 
      [self.executeResults setObject:state forKey:name]; 

      if (self.delegate) { 
      dispatch_sync(dispatch_get_main_queue(), ^{ 

       [self.delegate didExecuted: exec withResult:state userInfo:userInfo]; 
      )}; 
     }]; 
    } 
}); 
+0

感谢列弗但像你建议的UI被冻结......上的日志我看到所有的元素都执行,但用户界面停止的第一个元素上,并且用户不能回答,因为一切都冻结。 – jerrygdm

+0

这很奇怪。我很难看出为什么会这样。它会阻止'didExecuted:withResult:userInfo'方法中有'dispatch_sync(dispatch_get_main_queue(),^ {...})'调用,但是我想你已经检查过了。 –

+0

里面didExecuted方法委托有ony一个collectionView的reloadData ... – jerrygdm

相关问题