运行code后才异步函数执行完毕 [英] Run code only after asynchronous function finishes executing

查看:123
本文介绍了运行code后才异步函数执行完毕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的雨燕和X code一般和我找到了很多困难试图弄清楚这一点。

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

我开发利用 Parse.com 后端服务器的应用程序。为了不阻塞主线程,每当来自服务器的应用程序下载任何东西,它是在不同的线程完成,异步。然而,code的其余部分将继续在主线程上执行,而它崩溃的时候有它应该将数据从服务器尚未下载。我想知道如何调用函数来执行异步函数结束后,这对在不同文件中的函数来完成。

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.

我读到倒闭可能会帮助这一点,但我发现有语法很困难,一个解释是大大AP preciated。但是,任何方式将是非常有益的。

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.

感谢

推荐答案

好了,你只需调用函数的在异步回调的结束的。也就是说当异步回调已经结束 - 这是当异步回调否则一切都完了!因此,例如:

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.

例如:

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 ...
}

这篇关于运行code后才异步函数执行完毕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆