OperationQueue 执行时未调用操作子类 "main" 方法?
Operation subclass "main" method not getting called when OperationQueue executes?
我创建了一个继承自基础 class "Operation" 的子class "MyOperation"。我在 "MyOperation" 中添加了一个函数,它在 class 实例化时被调用。但是,当创建操作 class 并将其添加到操作队列时,不会调用 "main" 方法。
class MyOperation: Operation {
let iterations:Int
let tag:String
init(iterations:Int, tag:String) {
self.iterations = iterations
self.tag = tag
}
/**
When Operation queue begins execution it calls the main method without parameters, so any parameters needed should be passed through initializer.
**/
override func main() {
performCalculation()
}
func performCalculation(){
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<iterations {
self.doCalculations()
}
let end = CFAbsoluteTimeGetCurrent()
print("time for \(tag):\(end-start)")
}
func doCalculations() {
let x = 100
let y = x*x
let _ = y/x
}
}
我正在从 viewcontroller 的 ViewDidLoad 调用 MyOperation class。
override func viewDidLoad() {
super.viewDidLoad()
createOperationQueuewithMyOperationClass()
}
func createOperationQueuewithMyOperationClass(){
let operationQueue = OperationQueue()
operationQueue.addOperation {
MyOperation(iterations: 10000000, tag: "Operation-1")
}
operationQueue.addOperation {
MyOperation(iterations: 1000, tag: "Operation-2")
}
operationQueue.addOperation {
MyOperation(iterations: 100000, tag: "Operation-3")
}
}
你用错了addOperation
。您所拥有的只是将一个块操作添加到队列中,而该块正在创建该操作并且不对其进行任何操作。你想要这个:
operationQueue.addOperation(MyOperation(iterations: 100000, tag: "Operation-3"))
请注意缺少大括号 { ... }
,而是括号。
您正在呼叫 addOperation(() -> Void)
instead of addOperation(Operation)
我创建了一个继承自基础 class "Operation" 的子class "MyOperation"。我在 "MyOperation" 中添加了一个函数,它在 class 实例化时被调用。但是,当创建操作 class 并将其添加到操作队列时,不会调用 "main" 方法。
class MyOperation: Operation {
let iterations:Int
let tag:String
init(iterations:Int, tag:String) {
self.iterations = iterations
self.tag = tag
}
/**
When Operation queue begins execution it calls the main method without parameters, so any parameters needed should be passed through initializer.
**/
override func main() {
performCalculation()
}
func performCalculation(){
let start = CFAbsoluteTimeGetCurrent()
for _ in 0..<iterations {
self.doCalculations()
}
let end = CFAbsoluteTimeGetCurrent()
print("time for \(tag):\(end-start)")
}
func doCalculations() {
let x = 100
let y = x*x
let _ = y/x
}
}
我正在从 viewcontroller 的 ViewDidLoad 调用 MyOperation class。
override func viewDidLoad() {
super.viewDidLoad()
createOperationQueuewithMyOperationClass()
}
func createOperationQueuewithMyOperationClass(){
let operationQueue = OperationQueue()
operationQueue.addOperation {
MyOperation(iterations: 10000000, tag: "Operation-1")
}
operationQueue.addOperation {
MyOperation(iterations: 1000, tag: "Operation-2")
}
operationQueue.addOperation {
MyOperation(iterations: 100000, tag: "Operation-3")
}
}
你用错了addOperation
。您所拥有的只是将一个块操作添加到队列中,而该块正在创建该操作并且不对其进行任何操作。你想要这个:
operationQueue.addOperation(MyOperation(iterations: 100000, tag: "Operation-3"))
请注意缺少大括号 { ... }
,而是括号。
您正在呼叫 addOperation(() -> Void)
instead of addOperation(Operation)