操作キューを実装しようとしていますが、次のシナリオがあります。
NSOperation A
NSOperation B
NSOperation C
NSOperation D
NSOperationQueue queue
に追加A
し始めqueue
ます。
の実行中にA
データを取得する必要があり、必要なものが返されるまでB
続行できません。A
B
に依存する場合とB
依存する場合は、同じ状況が発生します。C
C
D
これを管理するために、それぞれNSOperation
に次のコードがあります。
NSOperation *operation; //This can be A, B, C, D or any other NSOperation
[self setQueuePriority:NSOperationQueuePriorityVeryLow]; //Set the current NSOperation with low priority
[queue addOperation: operation]; //Add the operation that I want to the queue
while(!operation.isFinished && !self.isCancelled){} //I need to wait the operation that I depend before moving on with the current operation
[self setQueuePriority:NSOperationQueuePriorityNormal]; //After the while, the other operation finished so I return my priority to normal and continue
if(self.isCancelled){ //If I get out of the while because the current operation was cancelled I also cancel the other operation.
[operation cancel];
}
私の問題は、3 つまたは 4 つのようなコードNSOperations
を待機して実行すると、while(!operacao.isFinished && !self.isCancelled){}
優先度が高くても重要な NSOperation が実行されないため、フリーズすることです。
私が試したこと
実行時に依存関係を追加しますが、私の NSOperation は既に実行されているため、何の効果もないようです。
操作をキューに追加する代わりに、何かを行うことができます
[operation start]
。動作しますが、現在の操作をキャンセルすると、開始した他の操作もキャンセルされますか?のようなことができます
while(!operacao.isFinished && !self.isCancelled){[NSThread sleepForTimeInterval:0.001];}
。それは機能しますが、これは正しい方法ですか?たぶん、より良い解決策があります。
この状況で、必要な操作が実行され、他の操作がバックグラウンドで待機することをどのように保証できますか? これを解決する正しい方法は何ですか?
キューを開始する前に依存関係を追加しない理由を誰かが私に質問した場合、それは、いくつかの条件が真の場合にのみ操作で他の依存関係が必要になるためです。実行時にのみ、他の操作が必要かどうかがわかります。
御時間ありがとうございます。