Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code only after asynchronous function finishes executing

I am relatively new to Swift and Xcode in general and am finding a lot of difficulty trying to figure this out.

I am developing an app that utilizes the Parse.com backend server. In order not to block the main thread, whenever the app downloads anything from the server, it is done on a different thread, asynchronously. However the rest of the code continues to execute on the main thread, and it crashes when the data it is supposed to have from the server has not downloaded yet. I would like to know how to call functions to run after the asynchronous function finishes, and this has to be done for functions in separate files.

I read that closures might help with this, but I found there syntax very difficult and an explanation would be greatly appreciated. But any way would be very helpful.

Thanks

like image 698
Acoop Avatar asked May 08 '15 22:05

Acoop


People also ask

How do you wait for an async function to finish?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Does async await stop execution?

An async function can contain an await expression, that pauses the execution of the function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

Do async functions run immediately?

An async function runs synchronously until the first await keyword. This means that within an async function body, all synchronous code before the first await keyword executes immediately.

Which is the correct function call in the case when the asynchronous action finishes successfully?

A callback function executes when an asynchronous operation completes. Here is an example of how a setTimeout function works: function printMe() { console.


2 Answers

Expanding on Matt's answer, you can make myMethod a method that takes a closure as a parameter:

​func​ ​myMethod​(​completionBlock​: (result: String) -> ()) 
{
    // ... code ...
    somebody.doSomethingWith(someObject, asynchronousCallback: {
        (thing, otherThing) in
        // ... do whatever
        completionBlock(thing)
    })
    // ... code ...
}
like image 41
Duncan C Avatar answered Nov 15 '22 20:11

Duncan C


Well, you simply call the function at the end of the asynchronous callback. That is when the asynchronous callback has ended - it is when everything else in the asynchronous callback has finished! So, for example:

func myMethod() {
    // ... code ...
    somebody.doSomethingWith(someObject, asynchronousCallback: {
        (thing, otherThing) in
        // ... do whatever
        // --> CALL THE FUNCTION!
    })
    // ... code ...
}

If the problem is that you do not know what function to call, you can configure your surrounding function / object so that someone can hand you a function which is then what you call in the spot where I said "call the function" in the above.

For example:

func myMethod(f:() -> ()) { // we receive the function as parameter
    // ... code ...
    somebody.doSomethingWith(someObject, asynchronousCallback: {
        (thing, otherThing) in
        // ... do whatever
        // --> CALL THE FUNCTION, by saying:
        f()
    })
    // ... code ...
}
like image 138
matt Avatar answered Nov 15 '22 21:11

matt