iOS - Swift - Function that returns asynchronously retrieved value
Yes, it is possible to do this. Its called a closure
, or more commonly a callback
. A callback
is essentially a function that you can use as an argument in another functions. The syntax of the argument is
functionName: (arg0, arg1, arg2, ...) -> ReturnType
ReturnType
is usually Void
. In your case, you could use
result: (image: UIImage?) -> Void
The syntax of calling a function with one callback in it is
function(arg0, arg1, arg2, ...){(callbackArguments) -> CallbackReturnType in
//code
}
And the syntax of calling a function with several callbacks is (indented to make it easier to read)
function(
arg0,
arg1,
arg2,
{(cb1Args) -> CB1Return in /*code*/},
{(cb2Args) -> CB2Return in /*code*/},
{(cb3Args) -> CB3Return in /*code*/}
)
If your callback function escapes the main function (the callback is called after the main function returns), you must add @escaping in front of the callback's argument type
You're going to want to use a single callback that will be called after the function returns and that contains UIImage?
as the result.
So, your code could look something like this
func imageFromFile(file: PFFile, result: @escaping (image: UIImage?) -> Void){
var image: UIImage?
file.getDataInBackgroundWithBlock() { (data: NSData?, error: NSError?) -> Void in
//this should be 'error == nil' instead of 'error != nil'. We want
//to make sure that there is no error (error == nil) before creating
//the image
if error == nil {
image = UIImage(data: data!)
result(image: image)
}
else{
//callback nil so the app does not pause infinitely if
//the error != nil
result(image: nil)
}
}
}
And to call it, you could simply use
imageFromFile(myPFFile){(image: UIImage?) -> Void in
//use the image that was just retrieved
}
What you want is exactly what the promise/future design pattern does. There are many implementations in Swift. I will use as an example the excellent BrightFutures library. (https://github.com/Thomvis/BrightFutures)
Here the code:
func imageFromFile(file: PFFile) -> Future<UIImage> {
let promise = Promise<UIImage>()
file.getDataInBackgroundWithBlock() { (data: NSData?, error: NSError?) -> Void in
if error != nil {
image = UIImage(data: data!)
// As soon as the method completes this will be called
// and triggers the future.onSuccess in the caller.
promise.success(image)
} else {
// This would trigger future.onFailure in the caller
promise.failure(error)
}
}
return promise.future
}
Explanation: what you basically do is creating a "promise" that there will be a result in the "future". And you are returning this future-promise immediately, before the async method completes.
The caller of this method will handle it like this:
func doSomethingWithTheImage() {
let future = imageFromFile(file: pffile)
future.onSuccess { image in
// do something with UIImage: image
}
future.onFailure { error in
// handle NSError: error
}
}
In the onSuccess handler you are doing all the stuff with the successfully downloaded image. If there is an error you handle it in the onFailure handler.
This solves the problem of returning "nil" and is also one of the best practices to handle asynchronous processes.