1

I'm new into programming in Swift and so far I'm downloading Json data as Strings and populate a UITableView. The images inside are url links. The problem is that the data are many, 53, so 53 url calls to get the images as well. At first, I was doing everything in the main thread so it was lagging a lot. Now I've put the code that does the http calls in an async method. The app does not lag but

1) The images don't download in order (I don't mind about that much although it would be nicer)

2) The download is slow, the memory hits around 250-270mb and the cpu around 40-50% while the network is around 500kb/s.

I don't own an iPhone to do a real check but with those numbers I see that the app uses a lot of resources. I wonder why the network is so slow though. Using 3g-4g must be faster and less stressing in my opinion and I don't know what the emulator is using.

So my question, is there any way for my app to go any faster or use less resources?

Below the code that puts the data on the TableView. It takes below 2 seconds to fill the table with the strings and a lot of time to download all the images.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "GameTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GameTableViewCell
    let qualityOfServiceClass = QOS_CLASS_BACKGROUND
    let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)

    cell.nameLabel?.text = games[indexPath.row].name
    cell.releaseDateLabel?.text = games[indexPath.row].releaseDate

    dispatch_async(backgroundQueue, {
        for _ in self.games{
            if let url = NSURL(string: self.games[indexPath.row].gameImage!) {
                if let data = NSData(contentsOfURL: url){
                    cell.photoImageView?.contentMode = UIViewContentMode.ScaleAspectFit
                    cell.photoImageView?.image = UIImage(data: data)
                }
            }
        }
    })
    return cell
}

2 Answers 2

1

You're downloading the images every time you want to show a cell. You need to use NSCache or any other third-party solution or something as simple as NSMutableDictionary for caching the images that downloads successfully so you don't download them every time.

You can also use the existing third-party solutions like AlamofireImage and SDWebImage which provide async downloading of images, showing placeholder image, and caching

Sign up to request clarification or add additional context in comments.

1 Comment

I will try it my way and if I can't I will add a third party library thanks for the info.
0

I was skeptical because I have never used an outside library but erasing 5-10 lines of code to just type the code below.. well, it saves a lot of time and trouble.

 if let url = NSURL(string: self.games[indexPath.row].gameImage!) {
        cell.photoImageView?.hnk_setImageFromURL(url)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.