2014-09-11 52 views
0

我有幾個應該返回委託響應的調用。這些呼叫可以並行進行。只有在所有呼叫完成後,我才能啓動程序。爲少數代表創建隊列

我正在考慮正確的方式來做到這一點,我的主要想法是設置一個整數,當一個委託完成時,該整數遞增,當它等於X我是開始。

問題是,如果增量是原子的,還是必須同步。

-(void)serverDelegate1:(NSMutableArray*)images 
    integer++; 
//check 

    -(void)serverDelegate2:(NSMutableArray*)images 
    integer++; 
//check 

    -(void)serverDelegate3:(NSMutableArray*)images 
    integer++; 
//check 

並設置一個超時,所以過了一段時間它開始了。

這可能是一個完全錯誤的方式,所以原諒我。

+0

將分別代表是同一類的一個單獨的實例:

的更多信息可以在Apple文檔中找到?如果這樣的話,在實例中遞增一個整數將不起作用;你需要從「外部」控制這個。 – trojanfoe 2014-09-11 08:32:58

+0

不,每個代表是另一個連接另一個服務器的類。 – Curnelious 2014-09-11 08:39:40

+0

是不是「是」?每個都是一個單獨的實例(相同或不同的類)? – trojanfoe 2014-09-11 08:57:36

回答

1

你有沒有想過使用併發隊列?就像這個例子:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_group_t group = dispatch_group_create(); 

// Add a task to the group 
dispatch_group_async(group, queue, ^{ 
    // Some asynchronous work 
}); 

// Do some other work while the tasks execute. 

// When you cannot make any more forward progress, 
// wait on the group to block the current thread. 
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); 

// Release the group when it is no longer needed. 
dispatch_release(group); 

您可以創建在委託隊列中,並等待三個任務完成? https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html

+0

非常感謝,看起來更專業。 – Curnelious 2014-09-11 10:26:44