OperationQueue 停止操作一段时间
OperationQueue stop operations for a little time
我用的是swift3.0.2。 Xcode8.3.1
我尝试使用 isSuspended = true
属性 但它不会停止操作。
当我取消一个 operation1
时,所有其他依赖于 operation1
的操作立即开始。
我希望他们等到我告诉他们。
请帮帮我!
编辑1:
意味着它不会停止操作:
我有操作队列oq
和三个操作:op1
、op2
、op3
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
为真时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's
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
}
}
游乐场示例:
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
我用的是swift3.0.2。 Xcode8.3.1
我尝试使用 isSuspended = true
属性 但它不会停止操作。
当我取消一个 operation1
时,所有其他依赖于 operation1
的操作立即开始。
我希望他们等到我告诉他们。
请帮帮我!
编辑1:
意味着它不会停止操作:
我有操作队列oq
和三个操作:op1
、op2
、op3
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
为真时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's
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
}
}
游乐场示例:
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