Rive state not updating the view
Hi all,
I’m seeing intermittent “stuck” state updates in the Rive iOS runtime. An enum input stops taking effect even though I set it from Swift, and the animation/state machine appears to remain in the previous state. When this happens, simply making the view disappear and reappear (navigate away/back) usually “unsticks” it and state changes start applying again.
Context / setup:
- SwiftUI app
- Two separate screens/views that both show the same Rive asset
- Each view has its own RiveViewModel (I originally shared one VM, but changed to two to avoid animation speed issues)
- I keep the two view models “in sync” by writing the same state changes to both.
Observed behavior:
- The issue most often occurs during navigation/transition where I hide View A and show View B (overlay), then return to View A.
- After returning to View A, updates to the enum input sometimes don’t visually change the state (it stays on the old state).
- If I navigate away so the Rive view is removed from the hierarchy and then come back, it usually, but not always, starts responding again.
What I’m updating:
- A databound enum input (chargingState) via instance.enumProperty(fromPath: "chargingState")?.value
- I also tried forcing evaluation by calling play() and advancing (e.g. riveView?.advance(delta:)), but the “stuck until reappear” behavior still happens intermittently.
Is there a known issue where databinding/state machine inputs are not consumed unless the artboard/state machine is actively advancing? And is there a recommended pattern for ensuring enum inputs reliably trigger state changes in SwiftUI when views appear/disappear (especially around transitions and overlays)?
Below is my code, please ignore the various log messages. The log messages confirm that the state is being set though.
enum RiveChargingState: String {
case idle
case waitingForAuth
case waitingToCharge
case charging
case warning
case disabled
}
extension RiveChargingState {
init(status: ChargingStatus) {
switch status {
case .carConnectedButNotCharging: self = .waitingToCharge
case .carNotConnected: self = .idle
case .charging: self = .charging
case .chargingStoppedOrFinished: self = .waitingToCharge
case .unknown: self = .idle
}
}
}
struct ChargerCardHeaderAnimationView: View {
// MARK: - Public configuration
private let chargingStatus: ChargingStatus
// MARK: - Rive runtime objects
private let riveViewModel: RiveViewModel
@State private var riveInstance: RiveDataBindingViewModel.Instance?
@State private var chargingStateProperty: RiveDataBindingViewModel.Instance.EnumProperty?
@State private var pendingState: RiveChargingState?
@Environment(\.chargerCardMode) private var envMode: ChargerCardMode
init(chargingStatus: ChargingStatus, riveViewModel: RiveViewModel) {
self.chargingStatus = chargingStatus
self.riveViewModel = riveViewModel
riveViewModel.fit = .fitHeight
}
var body: some View {
riveViewModel.view()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.task {
riveViewModel.fit = .fitHeight
riveViewModel.riveModel?.enableAutoBind { instance in
riveViewModel.fit = .fitHeight
riveInstance = instance
chargingStateProperty = instance.enumProperty(fromPath: "chargingState")
let latest = pendingState ?? RiveChargingState(status: chargingStatus)
pendingState = nil
pushToRive(latest, advanceToFinalState: true)
Log.ui.debug("(\(envMode.rawValue)) Initial charging state (applied on bind): \(latest.rawValue)")
}
}
.onChange(of: chargingStatus) { _, newValue in
let newState = RiveChargingState(status: newValue)
pendingState = newState
pushToRive(newState)
Log.ui.debug("(\(envMode.rawValue)) Charging state changed: \(newState.rawValue)")
}
}
private func pushToRive(_ state: RiveChargingState, advanceToFinalState: Bool = false) {
DispatchQueue.main.async {
Log.ui.debug("(\(envMode.rawValue)) Setting charging state1: \(state.rawValue) (Advance: \(advanceToFinalState))")
let instance = riveViewModel.riveModel?.stateMachine?.viewModelInstance ?? riveInstance
guard let instance else {
pendingState = state
return
}
Log.ui.debug("(\(envMode.rawValue)) Setting charging state2: \(state.rawValue) (Advance: \(advanceToFinalState))")
let prop = chargingStateProperty ?? instance.enumProperty(fromPath: "chargingState")
chargingStateProperty = prop
prop?.value = state.rawValue
// Added the two lines below to see if that could fix the issue
riveViewModel.riveView?.advance(delta: 1)
if advanceToFinalState {
riveViewModel.riveModel?.stateMachine?.advance(by: 240.0)
riveViewModel.riveModel?.artboard?.advance(by: 240.0)
}
}
}
}