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
}