2012-10-08 77 views
0

的結束通知我有NSOperationQueue 3 NSInvocationOperation在prepareForSegue回調,我完成所有異步任務後需要移動到另一個視圖控制器。 如何獲得有關在完成所有任務異步,只有在此之後移動到另一個屏幕通知?如何接收上的異步任務

我想這沒有成功:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

}); 

編輯:

看來,我不明白的東西:

-(void)initPurchase{ 

    [operationQueue cancelAllOperations]; 

    NSInvocationOperation *downloadImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageDownloader getInstance] selector:@selector(downloadImageSync:) object:@"http://.........jpg"]; 


    NSInvocationOperation *createImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageCreator getInstance] selector:@selector(createImage:) object:@"dsadsadsa"]; 


    NSInvocationOperation *saveImageOperation = [[NSInvocationOperation alloc] initWithTarget:[BSImageSaver getInstance] selector:@selector(saveImageAsPng:) object:[BSSharedObject getInstance].createdImage]; 

    [createImageOperation addDependency:downloadImageOperation]; 
    [saveImageOperation addDependency:createImageOperation]; 


    [operationQueue addOperation:downloadImageOperation]; 
    [operationQueue addOperation:createImageOperation]; 
    [operationQueue addOperation:saveImageOperation]; 


} 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    [[BSPopupManager getInstance]showWaitingPopup]; 


    dispatch_group_t group = dispatch_group_create(); 

    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
         ^{ 
          Purchase *purchase = [[Purchase alloc] init]; 
          [purchase initPurchase]; 
         }); 

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
     [[BSPopupManager getInstance] closeWaitingPopup]; 
     BSPurchaseViewController *purchaseViewController = [segue destinationViewController]; 
     purchaseViewController.pngImage = [BSSharedObject getInstance].createdImage; 


NSLog(@"2"); 
    }); 

    dispatch_release(group); 

} 
} 

我仍然得到的NSLog(@ 「2」) ;比我更早的保存圖像中第3 NSInvocationOperation

回答

3

可以使用dispatch_group_async()功能,如果你打算使用GCD來執行你的異步任務:Grand Central Dispatch (GCD) Reference

dispatch_group_t group = dispatch_group_create(); 

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
^{ 
    //Code here is executed asynchronously 
}); 

dispatch_group_notify(group, dispatch_get_main_queue(), 
^{ 
    //Do something when async has completed 
    //Note: You are not required to use the main 
    //queue if you aren't performing any UI work. 
}); 

dispatch_release(group); 
1

添加第四操作依賴於前三個是職位主線程上的NSNotification。像

NSOperation * completionOp = [NSBlockOperation blockOperationWithBlock:^{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:MyOperationsCompletedNotification object:... userInfo:nil]; 
    }); 
}]; 
[completionOp addDependency:op1]; 
[completionOp addDependency:op2]; 
[completionOp addDependency:op3]; 
[opQueue addOperation:op1]; 
[opQueue addOperation:op2]; 
[opQueue addOperation:op3]; 
[opQueue addOperation:completionOp]; 

是我一直留在代碼...,因爲你可能要填寫對象場比無其他東西(但它不應該是除了那些被證明是一個線程安全類的UIKit )。

可替換地,每個操作可以經由dispatch_async()-performSelectorOnMainThread:返回它的結果,但同樣,是不明智的參考塊中的線程不安全的UIKit類的實例(或作爲-performSelectorOnMainThread:的接收器或對象)。

有很多方法可以做到這一點這是爲線程安全的的GameKit例子—特別,任何可能最終在一個UIView或UIViewController中是危險的調用-release,因爲這可能會導致-dealloc被稱爲特別提到在後臺線程上。

0

你可以使用鍵 - 值觀察看你saveImageOperationisFinished財產。

0

您可以迅速3.通過dispatch_group當所有任務完成後可以得到通知做到這一點使用GCD。

let group = DispatchGroup() 

    group.enter() 
    run(after: 6) { 
     print(" 6 seconds") 
     group.leave() 
    } 

    group.enter() 
    run(after: 4) { 
     print(" 4 seconds") 
     group.leave() 
    } 

    group.enter() 
    run(after: 2) { 
     print(" 2 seconds") 
     group.leave() 
    } 

    group.enter() 
    run(after: 1) { 
     print(" 1 second") 
     group.leave() 
    } 


    group.notify(queue: DispatchQueue.global(qos: .background)) { 
     print("All async calls completed") 
}