rive-app / rive-app/rive-android
Q: Safe way to preload RiveFile outside of Compose context?
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
- Is there a recommended/safe way to preload
RiveFileinstances outside of Compose? - Is my workaround with
RiveWorker.pollMessages()in a coroutine safe for production use? - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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