Skip to main content
Under most circumstances a .riv file should load quickly and managing the RiveFile yourself is not necessary. But if you intend to use the same .riv file in multiple parts of your application, or even on the same screen, it might be advantageous to load the file once and keep it in memory.

Example Usage

To cache a Rive file, you can create a strong reference to a File object. This File can then be reused to create Rive objects.Artboards and state machines are unique when created using the create functions. This means that you can create a new Rive object with the same file, but with different (and unique) artboards and state machines.
// An example builder class that creates a new Rive object with a cached file, creating new and unique artboards and state machines for each Rive object.
class RiveBuilder {
    private let file: File

    init(file: File) {
        self.file = file
    }

    @MainActor
    func createRive(artboard: String? = nil, stateMachine: String? = nil) async throws -> Rive {
        let artboardInstance = try await file.createArtboard(artboard ?? .default)
        let stateMachineInstance = try await artboardInstance.createStateMachine(stateMachine ?? .default)
        return try await Rive(file: file, artboard: artboardInstance, stateMachine: stateMachineInstance)
    }
}

// Load and cache the file once
let file = try await File(source: ..., worker: Worker())

// Create a builder with the cached file
let builder = RiveBuilder(file: file)

// Create multiple Rive objects with different configurations
// Each Rive object is unique, but they all share the same cached File
let rive1 = try await builder.createRive() // Creates a unique artboard and state machine
let rive2 = try await builder.createRive() // Creates a unique artboard and state machine, behaving separately from the first Rive object
let rive4 = try await builder.createRive(artboard: "MainArtboard", stateMachine: "Walking") // Creates a unique artboard and state machine, behaving separately from the first three Rive objects
let rive5 = try await builder.createRive(artboard: "MainArtboard", stateMachine: "Idle") // Creates a unique artboard and state machine, behaving separately from the first four Rive objects