Davide Bertola
11/29/2020, 3:02 PMliveObject.observeAsState()
returns a State<T>
which is not something that can be subscribed toBrandon Trautmann
11/29/2020, 3:15 PMNote: observeAsState observes a LiveData<T> and returns a State<T> object that is updated whenever the LiveData changes. State<T> is an observable type that Jetpack Compose can use directly.
observeAsState will observe the LiveData only while it is in the Composition.
Davide Bertola
11/29/2020, 3:20 PM@Composable
fun <R, T : R> LiveData<T>.observeAsState(initial: R): State<R> {
val lifecycleOwner = LifecycleOwnerAmbient.current
val observer = remember { DisposableObserver<R, T>(initial, lifecycleOwner) }
observer.source = this
return observer.state
}
Halil Ozercan
11/29/2020, 3:44 PMDavide Bertola
11/29/2020, 3:45 PMAdam Powell
11/29/2020, 3:50 PMDavide Bertola
11/29/2020, 3:53 PMliveData.observe(this) { recompose() }
Adam Powell
11/29/2020, 4:44 PMsnapshotFlow {}
here: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/SnapshotFlow.ktval unregisterApplyObserver = Snapshot.registerApplyObserver { changed, _ ->
appliedChanges.offer(changed)
}
changed
is the set of snapshot-backed objects (e.g. mutableStateOf
) that changed in a single transactionDavide Bertola
11/29/2020, 4:47 PMAdam Powell
11/29/2020, 4:50 PMDavide Bertola
11/29/2020, 4:53 PMDavide Bertola
11/29/2020, 4:58 PM