Taras Tomchuk
Taras Tomchuk

Reputation: 331

CollectionView with lots of data Swift

I have collection view which is filled with lots of data from server:

Structure of cell:

Basically my collection layout is like - 3 cells in one row(multiple rows) with rounded corners and shadows. The problem I face is that when I have 30+ products received from server, my memory usage increases up to 200 MB and on older devices (iOS 10.*) my screen with collection view lags.

The way I'm receiving data is:

I'm using VIPER as architecture of my app and checked for memory leaks but everything seems to be fine. When I start app on other screens my memory usage is around - 30 MB. So the main issue I face is with loading image data. I have Googled allot and read many articles about optimizations but they provide mostly general advices and do not reflect such case as I have here. What they offer is:

While testing I found out, that if I populate collection view with clip image that is static - memory usage drops to ±100 MB

I would like to ask:

Thanks in advance for your help and have a nice weekend!

Upvotes: 1

Views: 933

Answers (2)

Nikdemm
Nikdemm

Reputation: 637

  • Kingfisher already contains all features what you need
  • You can use several levels of cache, e.g. if you reached limits of memory usage then it will cache data on a disk
  • Yes, if you save in memory a lot of images
  • You can disable downloading image if cell did end displaying on screen.

I think you need implement UITableViewDelegate func tableView:didEndDisplayingCell:forRowAtIndexPath

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.imageView.kf.cancelDownloadTask()
}

Kingfisher allows to configure image cache parameters (ImageCache) like:

maxCachePeriodInSecond, maxMemoryCost, maxDiskCacheSize

Therefore you can configure maxMemoryCost so that the memory won't more than 50mb.

Also Kingfisher clear memory cache automatically when a memory warning notification is received.

Upvotes: 3

Elhoej
Elhoej

Reputation: 751

Your problem is most likely that the image's size is big, try to compress them to JPEG format. You can play around with the compression quality to find a sweet spot.

guard let imageRep = UIImageJPEGRepresentation(image, 0.3) else { return }

Upvotes: 1

Related Questions