How I can return value from async block in swift

后端 未结 3 1567
无人及你
无人及你 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:18

    What you are describing with return self.mycallback() is something like in

    TypeScript:

    String text = await someTask();
    

    or in C#:

    String text = someTask().result;
    

    and from an async function:

    String text = await someTask();
    

    However this concept does not exist in swift (and i think Java too).

    The only other method I can think of other than using a completion handler, is passing the results to another function (NOTE: If you intend on working on the main thread/UI Thread you will get an exception - you will need to wrap your call in DispatchQueue.main.async {/*Do stuff...*/}) like so

        func startAsync {
            let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")!
            let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
                guard let data = data else { return }
                guard let jsonString = String(data: data, encoding: .utf8) else { return }
                DispatchQueue.main.async {
                    self.setResults(text: jsonString);
                }
            }
            task.resume()
        }
    
        func setResults(text: String?){
            textView.text = text;
        }
    

    full project: https://github.com/AppLogics/SwiftAsyncTaskWithoutCompletionHandler

    0 讨论(0)
  • 2020-12-02 03:23

    Your function would need a closure like so

    func getAppConfigFromDB(_ key: String, completion: @escaping (String?) -> Void) {
        backgroundthread.async {
            completion("string here")
        }    
    }
    

    When you call your function you would do

    getAppConfigFromDB("key") { (returnedString) in
        //returnedString is Optional("string here")
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题