小编Duc*_*Man的帖子

NSOperation maxConcurrentOperationCount = 1 仍然允许子类的多个操作

我对 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)

nsoperation nsoperationqueue swift

2
推荐指数
1
解决办法
941
查看次数

标签 统计

nsoperation ×1

nsoperationqueue ×1

swift ×1