How I can return value from async block in swift
Like @rmaddy said, you have no other way than to use completion handlers.
func getAppConfigFromDB(_ key: String, completion: @escaping ((String) -> Void)) {
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) in
// Use value to do something
}
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")
}