rive-app / rive-app/rive-android

Q: Safe way to preload RiveFile outside of Compose context?

Open
#438 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
538
Forks
66
PR merge metrics
No merged PRs in 30d

Description

Background

I'm working on pre-loading Rive files in advance to avoid loading delays during runtime. I need to do this outside of a Compose context.

Previous Approach

I previously used this approach:

val file = context.resources.openRawResource(fileId).use { File(it.readBytes()) }

However, this consumes too much memory for our use case.

Current Workaround

I've discovered that using RiveWorker with RiveFile.fromSource() reduces memory consumption significantly. However, RiveWorker appears to be Compose-specific. I've implemented a workaround that seems to work but feels like a hack:

suspend fun preloadRiveFile(@RawRes fileId: Int): RiveFile? {
    val riveWorker = RiveWorker()
    val riveFile = context.resources.openRawResource(fileId).use {
        RiveFileSource.Bytes(it.readBytes())
    }

    val riveFileResult = coroutineScope {
        val file = async {
            RiveFile.fromSource(riveFile, riveWorker)
        }

        while (file.isActive) {
            delay(100)
            riveWorker.pollMessages()
        }

        file.await()
    }
    
    return when (riveFileResult) {
        is Result.Success<RiveFile> -> riveFileResult.value
        else -> null
    }
}
Questions
  1. Is there a recommended/safe way to preload RiveFile instances outside of Compose?
  2. Is my workaround with RiveWorker.pollMessages() in a coroutine safe for production use?
  3. Are there any plans to provide an official API for pre-loading files for non-Compose contexts?

Any guidance would be greatly appreciated! 🙏

Environment:

Rive Runtime: 11.1.2
Platform: Android
Language: Kotlin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's RiveWorker, RiveFile.fromSource(), and RiveFileSource.Bytes entry points. Determine whether polling is supported safely outside Compose and what an official non-Compose preload API would need to guarantee; done means documented guidance or an accepted API direction.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.