[英]OperationQueue stop operations for a little time
I use swift 3.0.2. 我使用的是Swift 3.0.2。 Xcode 8.3.1
Xcode 8.3.1
I tried to use isSuspended = true
property but it doesn't stop operations. 我尝试使用
isSuspended = true
属性,但它不会停止操作。
When I cancel one operation1
all other operations that are dependent to operation1
immediately start. 当我取消一个
operation1
所有其他依赖于operation1
立即启动。
I want them to wait until I tell them. 我要他们等到我告诉他们。
Help me pleasee! 帮帮我吧!
Edit1: 编辑1:
Meaning it doesn't stop operations: 表示它不会停止操作:
I have operationqueue oq
and three operations: op1
, op2
, op3
我有操作
oq
和三个操作: op1
, op2
, op3
oq.addOperation(op1)
op2.addDependency(op1)
oq.addOperation(op2)
op3.addDependency(op2)
oq.addOperation(op3)
All three operations need 10 seconds to execute. 所有这三个操作都需要10秒才能执行。
After I add third operation I set isSuspended = true
添加第三次操作后,我将
isSuspended = true
设置isSuspended = true
My log:
op1 started
(-- set `isSuspended` to true)
(after 10 seconds)
op2 started
(after 10 seconds)
op3 started
I thought that op1 is executing when I set isSuspended
to true, it is OK. 我以为当我将
isSuspended
设置为true时,正在执行op1,就可以了。 But why op2 starts??? 但是为什么op2开始呢???
This is my File.swift
: 这是我的
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");
}
}
And this is ViewController's
viewDidLoad()
function: 这是
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)
}
Your computed property downloadQueue
creates a an independend queue on each call. 计算所得的属性
downloadQueue
在每个调用上创建一个独立队列。 Ie you are suspending the first created queue but the second and third queues are not influenced. 也就是说,您正在挂起第一个创建的队列,但是第二个和第三个队列不受影响。
To fix it, create the queue only once, maybe in init()
要修复它,只创建一次队列,也许在
init()
class PendingOps {
var downloadQueue: OperationQueue!
init() {
let queue = OperationQueue()
queue.qualityOfService = .background
queue.name = "Offline queue"
queue.maxConcurrentOperationCount = 1
downloadQueue = queue
}
}
Playground sample: 游乐场样本:
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
prints: 印刷品:
o1 started
true
o1 ended
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.