バックグラウンドでいくつかの非同期クエリを実行するためにNSOperation(現在Operationと呼ばれているもの)をサブクラス化しました。私はこれらを一度に1つずつ実行したいと思います。この目的のためにmaxConcurrentOperationCountを1に設定しましたが、依然としてキュー内の複数のオブジェクトを許可しています。NSOperation maxConcurrentOperationCount = 1サブクラスの複数の操作をまだ許可しています
私は右のクラス宣言の下に私のキューを宣言している:
let downloadQueue = OperationQueue()
次にビューに負荷が数を設定しました。そして、
downloadQueue.maxConcurrentOperationCount = 1
コードで操作サブクラスを呼び出す:
self.downloadQueue.addOperation(DownloadOperation(col: collectionView, index: indexPath, cell: cell))
サブクラス:
class DownloadOperation : Operation {
var collectionView: UICollectionView
var indexPath: IndexPath
var collectionCell: InnerCollectionCell
init(col: UICollectionView, index: IndexPath, cell: InnerCollectionCell) {
collectionView = col
indexPath = index
collectionCell = cell
}
let mainQueue = OperationQueue.main
override func main() {
if(isCancelled) {
return
}
///
/// Long query that gets objects in background
/// it is itself an async task
/// there is a completion block in the query
///
}
}
ダウンロード操作をdownloadQueueに追加し続けると、タスクは並行して実行され続けます(非同期)。これは、私が実行しているクエリ自体が非同期タスクなので、コードが先にスキップされ、非同期クエリがすべてのデータを取得する前に操作が完了したと仮定しているためですか?
この場合、NSOperationの完了を知らせる非同期クエリの完了を待つ方法はありますか?クエリ自体には完了ブロックがありますが、この場合にどのように利用するのかわかりません。