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 the leak of WebImage with animation and NavigationLink. #163

Merged
merged 2 commits into from
Feb 22, 2021

Conversation

dreampiggy
Copy link
Collaborator

The leak is because of @State which may cause reference cycle.

See result:

image

@dreampiggy dreampiggy added the webimage WebImage struct label Feb 22, 2021
if let animatedImage = image as? SDAnimatedImageProvider {
if let imagePlayer = SDAnimatedImagePlayer(provider: animatedImage) {
imagePlayer.animationFrameHandler = { (_, frame) in
self.currentFrame = frame
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here.

From Swift, the struct will not cause retain cycle. However, SwiftUI @State will keep an StorageLocation with an global mapping, which bind each View struct with the @State reference type. (Yes, each View struct will keep an reference)

So, if we write here, the retain cycle is:

View StorageLocation -> SDAnimatedImagePlayer -> animationFrameHandler -> View StorageLocation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By break off to use ImageManager a pure ObservableObject, this retain cycle can be avoided.

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

Successfully merging this pull request may close these issues.

None yet

1 participant