I basically have to methods, which are being called in my viewDidLoad. The first one gets the user\'s search preferences and saves the preferences to variables at the top. A
You can use Swift closures! They are made for that.
Please refer to the Apple guide: Closures
Here's the code you need in your particular case.
FinishedDownload is the closure. When getTheSearchLocationAndRange()
is called, its code is executed until the completed()
line which waits for all processes of the function to finish. Once the processes finish (downloads for example), completed()
calls the closure which activates the code defined in getTheSearchLocationAndRange { () -> () in
. Therefore, loadDataFromDatabase()
is only called once getTheSearchLocationAndRange()
has entirely finished executing and the data is present (not nil).
var searchLocation = String()
var searchLocationCoordinates = [String:Double]()
var searchRange = Int()
typealias FinishedDownload = () -> ()
override func viewDidLoad() {
super.viewDidLoad()
getTheSearchLocationAndRange()
}
func getTheSearchLocationAndRange(completed: FinishedDownload) {
// Code for searching Location Range HERE
completed()
}
getTheSearchLocationAndRange { () -> () in
loadDataFromDatabase()
}
I hope this solved your issue and answered your question :)
BTW, about the "leaving your GUI hanging" part, Alamofire takes automatically care of this for you. If you don't use Alamofire, then you will have to manually assign the asynchronous request to a background thread so your GUI doesn't become unresponsive.