如何在启动第二个函数之前等待函数在iOS / Swift上结束 [英] How to wait for a function to end on iOS/Swift, before starting the second one

查看:182
本文介绍了如何在启动第二个函数之前等待函数在iOS / Swift上结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上需要在viewDidLoad中调用的方法。第一个获取用户的搜索首选项并将首选项保存到顶部的变量。之后我想在第二个函数中访问和使用这些变量。但是现在当我想在第二个函数中访问它们时,变量总是为零。

I basically have to methods, which are being called in my viewDidLoad. The first one gets the user's search preferences and saves the preferences to variables at the top. After that I want to access and work with these variables in the second function. But now the variables are always nil when I want to access them in the second function.

我如何调整viewDidLoad,所以第二个函数只执行一次而我的数据请求已经成功执行了?

How do I need to adjust my viewDidLoad, so the second function is only executed once and my data request has been performed successfully?

var searchLocation = String()
var searchLocationCoordinates = [String:Double]()
var searchRange = Int()


override func viewDidLoad() {
    super.viewDidLoad()

    // Gets the user's search preference
    getTheSearchLocationAndRange()

    // Loads the data from the database
    loadDataFromDatabase()
}

到目前为止,我已经使用dispatch_asynch或完成处理程序读取了东西。也许有人可以发布一些我可以在我的viewDidLoad中使用的代码并使其工作?

I have read stuff so far with dispatch_asynch or completion handler. Maybe someone can post some code which I can use in my viewDidLoad and makes it work?

推荐答案

好的我找到了解决方案。我基本上在第一个函数的末尾调用了函数。

Okay I've found an solution. I basically called the function at the end of the first one.

所以基本上:

    var searchLocation = String()
    var searchLocationCoordinates = [String:Double]()
    var searchRange = Int()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Gets the user's search preference
        getTheSearchLocationAndRange()
    }

    func getTheSearchLocationAndRange() {
    // Code for getTheSearchLocationAndRange()

    loadDataFromDatabase()
    }

    func loadDataFromDatabase(){
    // Code for loadDataFromDatabase()
    }

这篇关于如何在启动第二个函数之前等待函数在iOS / Swift上结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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