我面臨addOperationWithBlock
的一些奇怪的結果。使用addOperationWithBlock的操作順序
我的功能看起來是這樣的:
-(void) myFunction{
NSLog(@"VISITED");
..
for (NSDictionary *Obj in myObjects)
{
[operationQueue addOperationWithBlock:^(void){
MyObject* tmp = [self tediousFunction:Obj];
// threadTempObjects is member NSMutableArray
[self.threadTempObjects addObject:tmp];
NSLog(@"ADDED");
}];
}
[operationQueue addOperationWithBlock:^(void){
[self.myArray addObjectsFromArray:self.threadTempObjects];
for(myObject *myObj in self.myArray)
{
// MAIN_QUEUE
[[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
[self updateUI:myObj];
}];
}
}];
[operationQueue addOperationWithBlock:^(void){
[[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
[self filterResults];
}];
}];
}
我的字典有4個值,因此增加的顯示在日誌中的4倍。 但, 當我檢查filterResults裏面,我看到有隻內myArray的對象。這意味着調用operationQueue的4倍並沒有在調用filterResults操作之前結束(儘管它稍後添加了!)
我認爲operationQueue是串行的,我可以指望當我添加操作它會在最後一次操作後立即添加。 所以奇怪的是,後果只有2個操作在數組中。 我錯過了什麼?謝謝
您正在使用這裏有2個不同的隊列,請分享他們的配置代碼,以便我們驗證他們確實是串行的(串行意味着maxConcurrentOperationCount爲1) –
您可能會對[GCD操作隊列](http://developer.apple.com/庫/ IOS /#文檔/ G ENERAL /概念/ ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html)。 –
您的操作隊列只有在串行時纔是串行的。 –