Haneke / HanekeSwift

A lightweight generic cache for iOS written in Swift with extra love for images.
Apache License 2.0
5.2k stars 591 forks source link

How to handle memory warning issues / crash when using hanekeswift to set images in collectionviewcell? #377

Open rlam3 opened 7 years ago

rlam3 commented 7 years ago

I'm using an API to pull JSON url objects back to my app and then mapping these object urls onto my cell with hanekeswift to simply set the image onto the cell. But I'm getting memory warning issues that may be related to hanekeswift or how hanekeswift is managing cache....

The following is a simple pseudo code of my code...

API call -> returns Objects in App -> append objects back into array of view controller -> reloadData -> Load cells with proper images and URL... -> If next pagination, repeat...

I remember reading how haneke is automagically managing my cache of these images... But I'd like to know if this would be a smart way of

// MyCollectionViewCell.swift

var myObject: Object{
   didSet{
      configureCell()
   }
}

func configureCell()
      self.topCoverImage.hnk_setImageFromURL(myObject.top_cover.url, placeholder: UIImage(), format: nil, failure: nil) {
            (image) -> () in

            // Set image after success
            self.topCoverImage.image = image

            // Stop animation when fully loaded
            self.activityIndicator.isHidden = true
            self.activityIndicator.stopAnimating()

        }
}
// MyCollectionViewController.swift

var myArrayOfObjects: [Objects] = []    /// Is this Array getting too big after X amount of pagination
var myPagination

func viewDidLoad{
   super.viewDidLoad()
   loadMyFeed()
}

func loadMyFeed(page:Int, limit:Int){

   api.getFeed(page:page, limit:limit){ (myObjects, pagination) in

      self.myArrayOfObjects.append(myObjects)
      self.myPagination = pagination

      self.collectionView.reloadData()

   }

}

func collectionView ... cellForItemAtIndexPath {

   // deque cell
   let cell = ...

   // set object for cell
   cell.myObject = myArrayOfObjects[indexPath.row]

   // detect if it is at the end of scroll
   self.loadMyFeed(page:nextPage, limit:someLimit)

}

I see that when my memory reaches above using 1GB ... the app crashes on my iPad but not my simulator because my PC has more memory to spare than does my iPad....

However, I'm looking for a solution to see if HanekeSwift can help me find a better way of managing my objects and then load them into the necessary cells with the proper images and url. I'm not too sure if this would be the most efficient way to handle image caching and my arrays together like this... Would love the insight on this.

Thanks!

reinder42 commented 7 years ago

Are you using the dequeueing mechanism of UICollectionView? And are you removing objects from myArrayOfObjects when they've moved off-screen? You could also profile your app with Instruments to see where the memory's gone. Lastly, I'd look into Realm for managing large amounts of data. Only keep in memory what's directly on screen. Haneke is generally used for caching data that's either requested from an indirect source, like a web image, or something that's resource-intensive or processed. It looks like you need to off-hand data to disk, not to memory.