I current have a set of asynchronous functions that are both called in the viewDidLoad()
. At the end of each function is a bool that is set from false to true upon completion of the function. There is also a conditional statement checking both function's bools that fires a third function. This conditional statement is in both functions (that I want called when both of the two have finished). Generally:
var checkOne = false
var checkTwo = false
func functionOne(){
//async stuff
checkOne = true
if checkOne == true && checkTwo == true{
functionThree()//will only run if both functionOne and functionTwo have been completed
}
}
func functionTwo(){
//async stuff
checkTwo = true
if checkOne == true && checkTwo == true{
functionThree()//will only run if both functionOne and functionTwo have been completed
}
}
func functionThree(){
//stuff
}
override func viewDidLoad() {
functionOne()
functionTwo()
}
This setup ensures that functionThree()
can only be run when both functionOne
and functionTwo
are done. If functionOne
finishes its async stuff before functionTwo()
and gets to the conditional to fire functionThree()
, it will not do it as checkTwo
has not been made true yet. Thus, when functionTwo()
's async stuff is done, it will fire functionThree()
. This works appropriately and has not caused an issue once. What I want to expressly avoid, though, is the async functions happening to finish, and therefore calling functionThree()
, at the exact same time. To do this I would like to set an NSLock()
, but, despite looking up documentation, I have zero clue how to do this as I need the same lock being handled by two different functions. Anyone have any ideas?
NSLock
method. It is surprisingly simple, but lets sayfunctionOne()
runs through enough such that it has calledlock.lock()
but not yetlock.unlock()
. IffunctionTwo()
then reaches itslock.lock()
, it cannot proceed yet (right?). My question is, doesfunctionTwo()
basically just wait there and then proceed when it can? ie does it sit on the linelock.lock()
untilfunctionOne()
unlocks the lock, and thenfunctionTwo()
proceeds? (Sorry I am just trying to understand exactly what's happening). – Galloromance