gpt4 book ai didi

swift - OperationQueue 停止操作一段时间

转载 作者:行者123 更新时间:2023-11-28 15:30:47 25 4
gpt4 key购买 nike

我使用的是 swift 3.0.2。 Xcode 8.3.1

我尝试使用 isSuspended = true 属性,但它不会停止操作。

当我取消一个 operation1 时,所有其他依赖于 operation1 的操作立即开始。

我希望他们等到我告诉他们。

请帮帮我!

编辑1:

意味着它不会停止操作:

我有操作队列 oq 和三个操作:op1op2op3

oq.addOperation(op1)
op2.addDependency(op1)
oq.addOperation(op2)
op3.addDependency(op2)
oq.addOperation(op3)

所有三个操作都需要 10 秒才能执行。

添加第三个操作后,我设置了 isSuspended = true

My log:
op1 started
(-- set `isSuspended` to true)
(after 10 seconds)
op2 started
(after 10 seconds)
op3 started

当我将isSuspended设置为true时,我以为op1正在执行,没问题。但是为什么op2开始了???

这是我的 File.swift:

import Foundation

class PendingOps {
// lazy var downloadInProgress = [Int: Operation]()
// current tracklist

var downloadQueue: OperationQueue {
let queue = OperationQueue()
queue.qualityOfService = .background
queue.name = "Offline queue"
queue.maxConcurrentOperationCount = 1
return queue
}
}


class op: Operation {
var nam:String = ""

init(nam: String) {
self.nam = nam
}

override func main() {
print("\(self.nam) started");

sleep(10)

print("\(self.nam) ended");
}
}

这是 ViewController 的 viewDidLoad() 函数:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let test = PendingOps()

let o1 = op(nam: "o1")
let o2 = op(nam: "o2")
o2.addDependency(o1)
let o3 = op(nam: "o3")
o3.addDependency(o2)

test.downloadQueue.addOperation(o1)

test.downloadQueue.isSuspended = true
print(test.downloadQueue.isSuspended)

test.downloadQueue.addOperation(o2)
test.downloadQueue.addOperation(o3)

}

最佳答案

您的计算属性 downloadQueue 在每次调用时创建一个独立队列。 IE。您正在暂停第一个创建的队列,但第二个和第三个队列不受影响。

要修复它,只创建队列一次,可能在 init()

class PendingOps {
var downloadQueue: OperationQueue!

init() {
let queue = OperationQueue()
queue.qualityOfService = .background
queue.name = "Offline queue"
queue.maxConcurrentOperationCount = 1
downloadQueue = queue
}
}

Playground 示例:

class PendingOps {
var downloadQueue: OperationQueue!

init() {
let queue = OperationQueue()
queue.qualityOfService = .background
queue.name = "Offline queue"
queue.maxConcurrentOperationCount = 1
downloadQueue = queue
}
}

class op: Operation {
var nam:String = "?"

init(nam: String) {
self.nam = nam
}

override func main() {
print("\(self.nam) started");

sleep(1)

print("\(self.nam) ended");
}
}

func didLoad() {
let test = PendingOps()

let o1 = op(nam: "o1")
let o2 = op(nam: "o2")
o2.addDependency(o1)
let o3 = op(nam: "o3")
o3.addDependency(o2)

test.downloadQueue.addOperation(o1)

test.downloadQueue.isSuspended = true
print(test.downloadQueue.isSuspended)

test.downloadQueue.addOperation(o2)
test.downloadQueue.addOperation(o3)

}

didLoad()

PlaygroundPage.current.needsIndefiniteExecution = true

打印:

o1 started
true
o1 ended

关于swift - OperationQueue 停止操作一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44764717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com