Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an asynchronous call prior to a segue to the next view controller (Swift)

Tags:

ios

swift

segue

I have a bunch of view controllers (embedded navigation controller), and I need to make some async calls before I segue to the next screen. The problem is that I need to make this call at the time I click the button mapped to the segue, but since the async call hasn't returned yet, the segue is made and the next view controller is shown. How do people do this kind of thing to prevent view controller navigation happening before the async call is returned?

like image 542
u84six Avatar asked Jan 22 '16 19:01

u84six


1 Answers

TL;DR You can create an @IBAction for the UIButton and inside the function make the async call and launch the segue manually in the completionHandler using the dispatch_async(dispatch_get_main_queue()) {}.

To create your segue manually just Ctrl+Drag from the UIViewController to the next one like in the following picture:

enter image description here

And then you will see to choose the type of segue like in this picture:

enter image description here

After you choose the Show type you can see the segue in the hierarchy of view in your left choose it to set the identifier for the segue like in this picture:

enter image description here You can handle your problem launching the segue manually with the function performSegueWithIdentifier, you can reference in the web to find a lot of tutorials about how to create a the segue manually using Interface Builder.

Once you segue has been created you can call it in anywhere you want, in your case as you will calling it inside a completionHandler of an async call you need to put in in the main thread using the dispatch_async(dispatch_get_main_queue()) {} function of Grand Central Dispatch.

Something like this code:

 @IBAction func buttonTapped(sender: AnyObject) {
    asyncFunction() {
        dispatch_async(dispatch_get_main_queue()) {
            self.performSegueWithIdentifier("someID", sender: sender)
        }
    }
}

Edit: Note that in Swift 3 the syntax has changed:

@IBAction func buttonTapped(_ sender: AnyObject) {
    asyncFunction() {
        DispatchQueue.main.async {
            self.performSegueWithIdentifier("someID", sender: sender)
        }
    }
}

I hope this helps you.

like image 148
Victor Sigler Avatar answered Nov 15 '22 05:11

Victor Sigler