【发布时间】:2015-10-17 02:38:42
【问题描述】:
我有多个功能应该按顺序进行。而function1 和function3 在后台线程中运行。他们的功能是findObjectsInBackgroundWithBlock。 Function 2 可以发生在主线程或后台线程上,但它应该在function 1 之后运行,而function 3 应该在function2 之后发生。我了解了NSOperation 和NSOperationQueue,但我不确定如何真正使用它。根据我的研究,我只能将类改为子类NSOperation。它是否正确?我想让每个函数都在NSOpeation 之下。以下是我认为正确的方式。这是正确的方法吗?将 function1() 放在NSOperation 中是否正确,如下所示?最后,当 function 1 & 3 之类的后端线程中发生某些功能时,它是否会影响 NSOperationQueue ?谢谢
var operationQueue = NSOperationQueue()
let operation1 : NSOperation = NSOperation(function1())
let operation2 : NSOperation = NSOperation(function2())
let operation3 : NSOperation = NSOperation(function3())
operation2.addDependency(operation1)
operation3.addDependency(operation2)
operationQueue.addOperation(operation1)
operationQueue.addOperation(operation2)
operationQueue.addOperation(operation3)
func function1 () {
//do something and save data in this class's property
}
func function2 () {
//get data from class's property(data from function1) and do something
//save the result in class's property
}
func function3 () {
//get data from class's property(data from function2) and do something
//save the result in class's property
}
【问题讨论】:
-
使用块操作。
标签: ios multithreading swift