Whats the most memory conserving method to create an infinite amount of sliding UICollectionViews

I'm creating a project which uses a

UICollectionView
. As the user selects a cell this slides them to another
UICollectionView
of similar nature. Ive been noticing that no matter how I go about this I wind up with mountains of memory usage.

I've been experimenting with placing

UICollectionView
's in full page
UICollectionViewCell
's so as to take advantage of the reusability of
UICollectionViews
. The downside of this approach has been memory retention as the CollectionViews are never fully deallocated. Ive also heard that it is not the best practice to put a
UICollectionView
in a
UICollectionView
.

I've experimented with

UIPageViewController
's containing
UIViewController
's with the
UICollectionView
inside. This is more efficient as the
UIViewController
's can be deallocated as the user swipes back however as long as the user continues to select cells and create new view controllers the memory grows unbounded looking like a mountain.

As a bit of a hybrid I attempted as well to put ViewControllers containing

UICollectionView
's on
UICollectionViewCell
's. This method seemed to work best but I had to deallocate the view controllers manually as the user swiped.

Are there any strategies or libraries anyone could recommend that would fit this problem. How can I keep the memory down. I understand I'll need some kind of reusable views.

Ive been looking into this Library so far thank you in advance.

Replies

If you mean an infinite grid - seen this link yet?


h ttps://itnext.io/infinite-grid-using-uicollectionview-155801e4f7f4

Your issue is with your action described as "...this slides them to...". If that means you are presenting a new child viewController as a child to the current viewController then you are leaving the current viewController in memory each time you create a new child. You need to either 1) dismiss the current viewController at the same time you present the new viewController and present that new viewController as a child to the parent of the current viewController or 2) not generate a new viewController but rather just reload the current viewController with data from the new collectionView.


And, if you are not presenting a new viewController each time but rather just creating a new collectionView within a viewController, the above applies with 'viewController' replaced by 'collectionView'.

Thank you for taking the time to answer our question. Do you believe we should nest the collectionViews within each other or put viewControllers inside the fullsized collectionView Cell or to simply use a pageViewController?

It depends on the transition from one display to the next. The simplest approach is to use the same viewController, change the data and just do a reloadData. But that transition can be too sudden. You may need to add some sort of overlay screen such as a transition to an all white screen, change the data, reload the collectionView and then fade the overlay screen.

But your choices are not what I was suggesting:


The UICollectuonView is in a viewController.

When you transition from one set of data to another you can:

1) do that all within the same viewController using the same UICollectionView by doing a reload data

2) transition to a new viewController that contains a UICollectionView with new data in it - this is a memory pig unless you swap in the new viewController differently, but the transition looks good

3) use a new UICollectionView in the same viewController - in this case you need to set the old UICollectionView to nil to avoid memory loss

Thank you for your detailed answer I appreciate your patience in helping us solve this. The first and third options you state are quite clever for conserving memory as all that would have to be updated is the datasource.


As you mention for the first option the animation could be a bit abrupt if we simply reloaded the datasource or performed batch updates on a singular collectionview. Our concern for the third option is that implementing an animation to give the user the illusion of swiping across screens could become a bit complicated to create. The second option seems to be closest to achieving our goals as it would have the best transition from one screen to the next thus having minimal need for animations. But as you stated it is a memory pig. How could we go about implementing the second option in a way that wouldn’t chew up as much memory?

If your UICollectionViewController is displayed inside a UIViewController I think you can just present a new UICollectionViewController within the same UIViewController and set the old UICollectionViewController equal to nil. I am not sure about this.


If your UICollectionViewController is being not being presented inside a UIViewController you could dismiss the current UICollectionController and then from its parent present a new UICollectionController.


Another way to manage any transition yourself is to cover the view with a UILabel, animate the alpha value of that UILabel from 0 to 1, make the change under the UILabel, and animate alpha from 1 back to 0.

Hello would you say it is similar to this?


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