Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

swift - Function does not wait until the data is downloaded

I have the following function which downloads an image from server;

func getImageFromServerById(imageId: String) -> UIImage? {
    let url:String = "https://dummyUrl.com/(imageId).jpg"
    var resultInNSDataformat: NSData!

    let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
        if (error == nil){
            resultInNSDataformat = data
        }
    }
    task.resume()
    return UIImage(data: resultInNSDataformat)
}

The function does not wait for the download task to be completed before returning the image. Therefore my app always crashes. Any ideas for how to wait for the download?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The other answer is not a good replacement for the code you already had. A better way would be to continue using NSURLSession's data tasks to keep the download operation asynchronous and adding your own callback block to the method. You need to understand that the contents of the download task's block are not executed before you return from your method. Just look at where the call to resume() is for further evidence.

Instead, I recommend something like this:

func getImageFromServerById(imageId: String, completion: ((image: UIImage?) -> Void)) {
    let url:String = "https://dummyUrl.com/(imageId).jpg"

    let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
        completion(image: UIImage(data: data))
    }

    task.resume()
}

Which can be called like this

getImageFromServerById("some string") { image in
    dispatch_async(dispatch_get_main_queue()) {
        // go to something on the main thread with the image like setting to UIImageView
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...