lottie-ios: AnimationView init blocks main thread for a noticeable amount of time

Check these before submitting:

  • The issue doesn’t involve an Unsupported Feature
  • [] This issue isn’t related to another open issue

This issue is a:

  • Non-Crashing Bug (Visual or otherwise)
  • [] Crashing Bug
  • [] Feature Request
  • Regression (Something that once worked, but doesn’t work anymore)

Which Version of Lottie are you using?

Lottie 3.0

What Platform are you on?

  • [] MacOS
  • iOS

What Language are you in?

  • Swift
  • [] Objective-C

Expected Behavior

Initializing an AnimationView should not block the main thread for a significant amount of time. The previous version LOTAnimationView did not block the main thread on init.

Actual Behavior

Initializing an AnimationView blocks the main thread long enough where it appears that the app has hanged.

Code Example

convenience init(fileUrl: URL, imageProvider: AnimationImageProvider? = nil, animationCache: AnimationCacheProvider? = LRUAnimationCache.sharedCache) {
    let animation = Animation.fileUrl(fileUrl, animationCache: animationCache)
    let provider = imageProvider ?? FilepathImageProvider(filepath: fileUrl.deletingLastPathComponent().path)
    self.init(animation: animation, imageProvider: provider)
}
let animationView = AnimationView(fileUrl: url)

Animation JSON

treasurechest-animation1.json.zip

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (1 by maintainers)

Most upvoted comments

The objects are being parsed on the main thread, we could offload those onto another thread and then feed them back into the animation view.

In the meantime you can initialize an animation view without the animation, and in another thread initialize the animation. You could also initialize the animation object early, and let the animation cache provide it to the animation view on allocation. This is a bit of a work around but would help a lot with large animations.

Even i faced the same issue, view gets freeze on time of loading the animation since trying to load big json. I tried to initialise the animation in background thread and then assign to animation view in main thread which removes the freeze.

self.showProgressView() DispatchQueue.global(qos: .userInitiated).async { let animation = Animation.named(self.type.animation) DispatchQueue.main.async { self.hideProgressView() self.animationView.animation = animation self.play() } self.animation = animation }

The objects are being parsed on the main thread, we could offload those onto another thread and then feed them back into the animation view.

In the meantime you can initialize an animation view without the animation, and in another thread initialize the animation. You could also initialize the animation object early, and let the animation cache provide it to the animation view on allocation. This is a bit of a work around but would help a lot with large animations.

My original problem was that the new page couldn’t be presented immediately while the Lottie JSON file was being loaded on the new page, so there was a hiccup on page transition. I’ve tried loading the JSON file in a different thread and it did help with the transition, but had its problems, because I was still using the main thread. I’ve tried using a background thread, but Lottie crashed, saying it required the main thread. So, now I can make it so the new page is presented before Lottie loads the JSON file but the user can’t leave the page before that loading is finished (and the whole page is unresponsive during the loading).

Just to be sure I don’t misunderstand something: does Lottie indeed require the main thread when loading/parsing the animation file?

Some additional info. I am loading the file from the bundle and this call blocks the main thread: animationView.animation = Animation.named("someAnimation"). The file size is pretty big - around 6 MB. Another thing you suggested was to load the file early, but if it still has to be done on the main thread, it would still block the whole UI no matter when I load it…