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

白昼怎懂夜的黑 提交于 2019-12-05 05:01:02

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:

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

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:

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.

It sounds like you're looking for it to run synchronously; so if you're using GCD call dispatch_sync instead of dispatch_async when putting the heavy lifting on the background thread, and when you need the segue to happen put things back onto the main thread by calling dispatch_sync(dispatch_get_main_queue(), ^{ // handle error or segue }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!