You can use Swift closure! They are made for this.
Refer to the Apple manual: Closures
Here is the code you need in your specific case.
FinishedDownload is a close. When getTheSearchLocationAndRange() is called, its code is executed until the completed() , which waits for the completion of all the processes of the function. After the processes (for example, loading) are completed() causes a closure that activates the code defined in getTheSearchLocationAndRange { () -> () in . Therefore, loadDataFromDatabase() is called only once when getTheSearchLocationAndRange() completely completed execution, and the data is present (not zero).
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 solves your problem and answers your question :)
By the way, about βleaving your hanging GUIβ part, Alamofire automatically takes care of this for you. If you are not using Alamofire, you will have to manually assign an asynchronous request to the background thread so that your GUI does not stop responding.
source share