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 issue where LottieView animation would restart from beginning after backgrounding app #2237

Merged
merged 2 commits into from
Dec 1, 2023

Conversation

calda
Copy link
Member

@calda calda commented Nov 22, 2023

This PR fixes an issue where the SwiftUI LottieView would always restart playback from the beginning after backgrounding the app, even when using a LottieBackgroundBehavior like continuePlaying or pauseAndRestore.

When using the default LottieView.init(animation:) initializer, the animation expression is recomputed on every view update. Since these calls include a AnimationCacheProvider, this simply returns the existing LottieAnimation instance rather than actually loading the animation from disk on every update.

DefaultAnimationCache is backed by an NSCache. The issue here is that NSCache actually evicts all of the cached items when the app is backgrounded (regardless of if there is memory pressure or not). I hadn't heard about this behavior before now!

This means that after backgrounding the app and then foregrounding it again, on the following SwiftUI view update:

  • the animation will no longer be present in the cache
  • the animation will be loaded from disk, resulting in a new LottieAnimation instance
  • LottieView will call view.loadAnimation(animationSource)
  • The "new" animation will start playing from the beginning.

We can fix this by using the LRUCache library as a replacement for NSCache. LRUCache works very similarly to NSCache, and automatically handled responding to memory pressure. LRUCache doesn't include this "reset when backgrounded" behavior, though, which avoids this specific issue.

Before After
2023-11-22 11 52 28 2023-11-22 11 51 16

Copy link

emerge-tools bot commented Nov 22, 2023

1 build increased size

Name Version Download Change Install Change
SizeTest
com.airbnb.lottie.sizetest.iOS
1.0 (1) 744.6 kB ⬆️ 3.7 kB (0.5%) 2.2 MB ⬆️ 10.8 kB (0.5%)

SizeTest 1.0 (1)
com.airbnb.lottie.sizetest.iOS

⚖️ Compare build
⏱️ Analyze build performance

Total install size change: ⬆️ 10.8 kB (0.5%)
Total download size change: ⬆️ 3.7 kB (0.5%)

Largest size changes

Item Install Size Change
📝 Lottie.LRUCache.setValue(forKey,cost) ⬆️ 1.8 kB
📝 Lottie.LRUCache.init(totalCostLimit,countLimit,notificationCenter... ⬆️ 932 B
📝 Lottie.LRUCache.value(forKey) ⬆️ 596 B
Other ⬆️ 7.4 kB

Image of diff


🛸 Powered by Emerge Tools

@fatihdurmaz
Copy link

thanks

Copy link
Contributor

@miguel-jimenez-0529 miguel-jimenez-0529 left a comment

Choose a reason for hiding this comment

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

LGTM, TIL NSCache clears its content after going to background.

@calda calda merged commit b3d81b4 into master Dec 1, 2023
16 checks passed
@calda calda deleted the cal--LRUCache-2236 branch December 1, 2023 23:10
cgrindel-self-hosted-renovate bot added a commit to cgrindel/rules_swift_package_manager that referenced this pull request Dec 4, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [airbnb/lottie-spm](https://togithub.com/airbnb/lottie-spm) | patch |
`from: "4.3.3"` -> `from: "4.3.4"` |

---

### Release Notes

<details>
<summary>airbnb/lottie-spm (airbnb/lottie-spm)</summary>

###
[`v4.3.4`](https://togithub.com/airbnb/lottie-spm/releases/tag/4.3.4)

[Compare
Source](https://togithub.com/airbnb/lottie-spm/compare/4.3.3...4.3.4)

#### What's Changed

- Fix parsing regression in 4.3.0 from addition of parsing layer effects
by [@&#8203;calda](https://togithub.com/calda) in
[airbnb/lottie-ios#2208
- Remove old animation layer when creating a new animation layer by
[@&#8203;junjielu](https://togithub.com/junjielu) in
[airbnb/lottie-ios#2214
- Add configuration setting to remove animated bounds changes by
[@&#8203;thedrick](https://togithub.com/thedrick) in
[airbnb/lottie-ios#2218
- Change pod dependencies `SwiftUI` and `Combine` to `weak` to ensure
compatibility with iOS 12 by
[@&#8203;florianrhein](https://togithub.com/florianrhein) in
[airbnb/lottie-ios#2219
- Fix issue where Repeater would be ignored if not at top level by
[@&#8203;calda](https://togithub.com/calda) in
[airbnb/lottie-ios#2221
- Replace `UIScreen.main` to get display scale on iOS 13.0 and later
([#&#8203;2215](https://togithub.com/airbnb/lottie-spm/issues/2215)) by
[@&#8203;hyun99999](https://togithub.com/hyun99999) in
[airbnb/lottie-ios#2216
- Dispatch dot lottie file loading onto a single serial queue by
[@&#8203;erichoracek](https://togithub.com/erichoracek) in
[airbnb/lottie-ios#2229
- Clean up unused property in InvertedMatteLayer by
[@&#8203;hanton](https://togithub.com/hanton) in
[airbnb/lottie-ios#2241
- Fix issue where LottieView animation would restart from beginning
after backgrounding app by [@&#8203;calda](https://togithub.com/calda)
in
[airbnb/lottie-ios#2237

**Full Changelog**:
airbnb/lottie-ios@4.3.3...4.3.4

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4xMDAuMCIsInVwZGF0ZWRJblZlciI6IjM2LjEwMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: Self-hosted Renovate Bot <361546+cgrindel-self-hosted-renovate[bot]@users.noreply.github.enterprise.com>
@nmduong99

This comment was marked as off-topic.

iago849 pushed a commit to atteamapps/lottie-ios that referenced this pull request Feb 8, 2024
MoroziOS pushed a commit to MoroziOS/tmg-lottie-ios that referenced this pull request May 22, 2024
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.

None yet

4 participants