How I can return value from async block in swift

后端 未结 3 1581
无人及你
无人及你 2020-12-02 02:49

Please have a look into the code below:

backgroundthread.async {
    return self.mycallback() //return string, int etc
}

I want to return a

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 03:34

    Like @rmaddy said, you have no other way than to use completion handlers.

    func getAppConfigFromDB(_ key: String, completion: @escaping ((String) -> Any)) {
        let value = String()
        backgroundthread.async {
            let inst = AppConfigDB.init(_APP_CONFIG_DB_PATH)
            value = inst.getConfigurationInfo(key) // I want to return from here.
            completion(value)
        }
    }
    

    You call the method like this.

    getAppConfigFromDB("") { (value) -> Any in
        // Use value to do something
    }
    

提交回复
热议问题