Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retain cycle between cell and view controller #1725

Conversation

DreamingInBinary
Copy link

In reference to this issue.

@CLAassistant
Copy link

CLAassistant commented Nov 7, 2019

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Jordan Morgan seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@bolsinga
Copy link
Contributor

Please add a unit test that verifies the cycle is gone. See #1429 for an example. Thanks!

@rahul-malik
Copy link
Contributor

@DreamingInBinary Mind updating the PR with a test and signing the CLA so we can merge this?

@DreamingInBinary
Copy link
Author

@rahul-malik Sorry about the radio silence - had a few things come up and went out for the holidays. I'll get this in this week 👍

@DreamingInBinary
Copy link
Author

@rahul-malik tests added 👍

Podfile.lock Outdated

COCOAPODS: 1.6.0
COCOAPODS: 1.8.4
Copy link
Contributor

@bolsinga bolsinga Apr 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't expect to see podfile changes with this diff.

Copy link
Contributor

@bolsinga bolsinga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the pod file changes, and things look good. Thanks!

@DreamingInBinary
Copy link
Author

@bolsinga I wasn't sure of the best way to undo the podfile.lock changes, so I updated from the lock file from Master. Typically if I have issues with a lock file, I delete it and run pod install again but I wasn't sure if removing the file would cause any other issues 🤔

@bolsinga
Copy link
Contributor

bolsinga commented Jun 6, 2020

@bolsinga I wasn't sure of the best way to undo the podfile.lock changes, so I updated from the lock file from Master. Typically if I have issues with a lock file, I delete it and run pod install again but I wasn't sure if removing the file would cause any other issues 🤔

I think you need to merge with master...

@@ -1390,8 +1392,6 @@
83A7D95D1D446A6E00BF333E /* ASWeakMapTests.mm */,
CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.mm */,
695BE2541DC1245C008E6EA5 /* ASWrapperSpecSnapshotTests.mm */,
407B8BAD2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm */,
CC698826247855F200487428 /* UIImage+ASConvenienceTests.mm */,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's close, but it looks like your diff removes this file. If you reset the project file back to master and re-add ASCellNodeTests.m, I think it will be good to go!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bolsinga Sorry for the back and forth here Greg!

So I took master's .pbxproj, and then added back in my new file (ASCellNodeTests.mm) and it doesn't appear to remove ASLayoutSpecUtilitiesTests.mm or UIImage+ASConvenienceTests.mm but rather seems to shift where they are in that particular file if I'm seeing things correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Sorry for my delay in finding your response and update.

@bolsinga
Copy link
Contributor

Looks like the license still needs to be accepted. I’ll land once that is complete. Thanks!

@DreamingInBinary
Copy link
Author

@bolsinga Done!
Screen Shot 2020-06-18 at 9 51 50 AM

@bolsinga
Copy link
Contributor

@bolsinga Done!
Screen Shot 2020-06-18 at 9 51 50 AM

In the UI I see, it still says "Jordan Morgan" has not signed this, but "DreamingInBinary" has. Maybe those two accounts are not fully associated with each other? I don't know why there are 2 associated with this PR in the first place...

@mycroftcanner
Copy link

@bolsinga did you manage to sign?

@bolsinga
Copy link
Contributor

@bolsinga did you manage to sign?

Hi @mycroftcanner It's @DreamingInBinary that needs to sign the CLA. @DreamingInBinary would you be able to remove the unsigned committer, or sign them? Thanks!

@foxware00
Copy link
Contributor

foxware00 commented Nov 13, 2020

@bolsinga @DreamingInBinary Interested in the PR, however, I think it has an unwanted side effect.

I've got an ASPagerNode setup and as I move out of range of a pager it's being retained indefinitely. As I'm using the AsPagerNode as one of my main tabs, this memory never gets freed.

I've used the fix applied in the PR. However, there seem to be unwanted side effects.

The ASPagerNode and I believe all ASCollectionNode's initialize their dataset and expect all nodes to be left around. The view controller included. The problem is, this now (I think correctly, deinit's each view controller as it goes off-screen). The problem is the ASPagerNode never re-creates it so you end up with empty pages. See the ASPagerNode documentation below. It seems to indicate that it's not called when the nodes are about to display and seems to be called in one batch as the dataset is applied. This means once deallocated pages, they're not able to be re-created.

I'm curious on two fronts, this fix in the PR seems correct to me. Nodes not in the range, that have disappeared should be allowed to deinit and free up memory. They should then be recreated. Neither the current logic or the PR allow that. Maybe a tweak is needed in re-creating nodes as they come into view.

@nguyenhuy @rahul-malik might be able to shed some light, but it looks like this PR has unwanted side effects or at the very least is a breaking change based on the assumptions of ASCollectionNode's allocation. I'd love to understand this a little more.

**
 * This method replaces -collectionView:nodeForItemAtIndexPath:
 *
 * @param pagerNode The sender.
 * @param index     The index of the requested node.
 * @return a node for display at this index. This will be called on the main thread and should
 *   not implement reuse (it will be called once per row).  Unlike UICollectionView's version,
 *   this method is not called when the row is about to display.
 */
- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index;

As mentioned above, since ASCellNodes are not meant to be reused, they have a longer lifecycle compared to the view or layer that they encapsulate or their corresponding UICollectionViewCell or UITableViewCell. ASCellNodes are deallocated when they are no longer used and removed from the container node. This can occur after a batch update that includes a reload data or deletion, or after the container node is no longer used and thus released.

Furthermore, the current implementations of ASCollectionNode and ASTableNode allocate all cell nodes as soon as they are inserted. That is, if clients perform a batch update that inserts 100 items into a collection node, the collection node will allocate 100 cell nodes as part of the processing of the batch update. It will also perform a layout calculation on each of the new cell nodes. Thus, at the end of the process, the collection node will manage 100 nodes more and these nodes have calculated layouts ready to be used.

From here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants