I have multiple functions which should be happening in order. And function1
and function3
run in back thread. Their functions are findObjectsInBackgroundWithBlock
. Function 2
can be happening on main thread or on back thread, but it should run after function 1
and function 3
should happen right after function2
. I got to know about NSOperation
and NSOperationQueue
but I'm not sure how to really use it. Based on what I researched, I can only make class to subclass NSOperation
. Is this correct? I would like to make each functions to be under NSOpeation
. Below is the way I think to be correct. Is this right way to approach? And is it correct to put function1() inside of NSOperation
like below? Lastly, does it affect NSOperationQueue
when some functions are happening in back thread like function 1 & 3
? Thank you
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
}