1

I've been working on a project of using two UICollectionView's I have on that is the MainViewController with full screen cells that scroll horizontally and one that sits on that full screen cell and scrolls vertically. I have a functionality that adds as well as deletes cells. When a user taps and one of the pages of the MainViewController is deleted using the code below the memory that grew as cells were added is still being retained. Is there something Im doing wrong. All of my delegates are weak references and none of my cells have self referencing closures. Am I doing something wrong thank you for you help

Here is the full project

https://github.com/TheRedCamaro30/Leaky-Nested-UICollectionViews

Add or Remove Cell Delegate

func addCell(){
    self.mainCollectionView.performBatchUpdates({
        guard let last = arr.last else{
            return
        }
        arr.append(last + 1)
        let lastIndex = IndexPath(item: last, section: 0)
        self.mainCollectionView.insertItems(at: [lastIndex])
    }) { (completed) in
        print("Batch updates completed, performed successfully: \(completed)")
    }
    }

func removeCell() {
    self.mainCollectionView.performBatchUpdates({
    arr.popLast()
        self.mainCollectionView.deleteItems(at: [IndexPath(item: arr.count - 1, section: 0)])

    }) { (competed) in
        print("Perform batch updates completed")
    }
}

Full Sized Cell Cell Population

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FullPageCell.reuseIdentifier, for: indexPath) as? FullPageCell else{
        assertionFailure("Fatal Error FullPageCell not dequed")
        return UICollectionViewCell()
    }
    cell.backgroundColor = UIColor.green
    cell.cellTappedDelegate = self

    return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if let cell = cell as? FullPageCell{
            cell.setUpCollectionView()
    }
}

SubCollectionView sitting on Full Page Cell Population

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SmallCell.reuseIdentifier, for: indexPath) as? SmallCell else{
        assertionFailure("Fatal Error FullPageCell not dequed")
        return UICollectionViewCell()
    }
    cell.backgroundColor = UIColor.yellow
    cell.imageView.image = self.image
    return cell
}

SetUpCollectionView

 func setUpCollectionView(){
   let view = self.contentView

   let layout = UICollectionViewFlowLayout()
   layout.minimumLineSpacing = 1
   layout.minimumInteritemSpacing = 1
   layout.scrollDirection = .vertical
   layout.itemSize = CGSize(width: (view.bounds.width - 2)/3, height: (view.bounds.width - 2)/3)

   collectionView = UICollectionView(frame:view.bounds, collectionViewLayout: layout)


   collectionView.dataSource = self
   collectionView.delegate = self
   collectionView.register(SmallCell.self, forCellWithReuseIdentifier: SmallCell.reuseIdentifier)
   collectionView.backgroundColor = UIColor.white

   self.collectionView.isPagingEnabled = true

   view.addSubview(collectionView)

   collectionView.translatesAutoresizingMaskIntoConstraints = false
   collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
   collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
   collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
   collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
 }
  • Please add code to populate data into collectionview. – Hitesh Surani Mar 26 '19 at 05:35
  • @iMHiteshSurani please see added code – TheRedCamaro3.0 3.0 Mar 26 '19 at 13:43
  • Just comment cell.cellTappedDelegate = self line then Execute code. I think memory issue is due to delegate. You have not manage properly – Hitesh Surani Mar 26 '19 at 18:55
  • @iMHiteshSurani Thank you for your continued help. The cellTappedDelegate is a weak reference in the collectionViewCell. I use it to detect when a user has tapped a cell in the collectionView inside of the collectionViewCell without it I wouldn't be able to trigger my removeCell function that calls performBatchUpdates. – TheRedCamaro3.0 3.0 Mar 26 '19 at 19:14
  • What is setUpCollectionView() method do – Hitesh Surani Mar 27 '19 at 04:08
  • @iMHiteshSurani Ive added the function in, setUpCollectionView sets the value of the collectionView along with its layout delegate and datasource and adds constraints to the collectionView – TheRedCamaro3.0 3.0 Mar 27 '19 at 15:27
  • This one has a different mechanism of reusing. There are at most 4 page of FullPageCell and ~120 small cells. so even you delete them does not mean they will release themselves. The peak memory is around ~37M and won't go up. So it's safe. – E.Coms Mar 28 '19 at 22:21

1 Answers1

0

To insert, delete, or move a single section or item, you must follow these steps:

  1. Update the data in your data source object.

  2. Call the appropriate method of the collection view to insert or delete the section or item.

Just replace removeCell method with below code.

func removeCell() {
    self.mainCollectionView.performBatchUpdates({
        arr.popLast()
        self.mainCollectionView.deleteItems(at: [IndexPath(item: arr.count - 1, section: 0)])
    }) { (competed) in
        print("Perform batch updates completed")
        //If it is not worked then reload collectionview
        //self.mainCollectionView.reloadData()
    }
}

Please refer below references.

Inserting, Deleting, and Moving Sections and Items

Collection View Programming Guide for iOS

Community
  • 1
  • 1
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • Thank you for your response, I tried making the changes to conform to the recommended practices but Im still noticing the memory is not dropping after a cell has been deleted using batch updates. – TheRedCamaro3.0 3.0 Mar 26 '19 at 05:08