我对 NSOperation(现在称为操作)进行了子类化,以在后台执行一些异步查询。我希望一次执行一个。为此,我将 maxConcurrentOperationCount 设置为 1,但它仍然允许队列中存在多个对象。
我已经在类声明的正下方声明了我的队列:
let downloadQueue = OperationQueue()
Run Code Online (Sandbox Code Playgroud)
然后在视图中加载设置了计数:
downloadQueue.maxConcurrentOperationCount = 1
Run Code Online (Sandbox Code Playgroud)
然后在代码中调用Operation子类:
self.downloadQueue.addOperation(DownloadOperation(col: collectionView, index: indexPath, cell: cell))
Run Code Online (Sandbox Code Playgroud)
还有我的子类:
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 …Run Code Online (Sandbox Code Playgroud)