cs193p – Project #4 Assignment #4 Task #9

Daniel FR at the German language Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or CC BY-SA 1.0 (http://creativecommons.org/licenses/by-sa/1.0)], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

You must not block the main thread of your application with a network request at any time.

Hopefully, we took care of this already in our “new” code. But there is a legacy from the code of the lecture which needs a minor tweak. Move the download of the profile images to an asynchronous queue, when the images has been download, check if the image is still valid (the tweet for this image might not be on screen any more) and set the image:

    func updateUI() {
        ...
            self.tweetProfileImageView?.image = nil
            if let profileImageURL = tweet.user.profileImageURL {
                dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
                    let imageData = NSData(contentsOfURL: profileImageURL)
                    dispatch_async(dispatch_get_main_queue()) {
                        if profileImageURL == tweet.user.profileImageURL {
                            if imageData != nil {
                                self.tweetProfileImageView?.image = UIImage(data: imageData!)
                            }
                        }
                    }
                }
            }
            ...
}

The complete code for task #9 is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.