Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
datastore-core and datastore-preferences have dependencies on android for SharedPreferencesMigrations and convenience constructors. We should extract these dependencies so users can use this in kotlin-only parts of their code.
[Deleted User] <[Deleted User]> #3
Would this enable Kotlin Multiplatform usage? Or just Kotlin/JVM in non-Android settings?
mg...@google.com <mg...@google.com> #4
This work is intended to support kotlin-only modules in android projects using Kotlin/JVM. We haven't tested this in non-android settings and haven't built it using Kotlin Multiplatform.
Description
Component used: lifecycle-viewmodel-savedstate
Version used: 2.5.0-rc01
Description
Recently,
SavedStateHandle
has added support forStateFlow
as a coroutines-based alternative to itsLiveData
support. However, the new API has a design decision that is significantly more fragile by comparison. First, let’s clarify the main difference:LiveData
exposes aMutableLiveData
.StateFlow
exposes a non-mutableStateFlow
.Now, a simple example can illustrate how code using these APIs would look:
From the examples above, consider what might happen if:
set
receives the wrong type for theStateFlow
.StateFlow
s, and we use the wrong keys in theset
call.This brings us to the drawbacks of forcing the
StateFlow
/manualset
API:StateFlow
of one type and callset
with a different type. There is zero type safety between keys and/or type betweenset
and theStateFlow
.key
must be tracked in every place aset
call occurs.StateFlow
).ViewModel
, the more keys and types we need to track, increasing its error-proneness.Ideally, calling
emailFlow.value = newValue
should work exactly like callingset
directly on aSavedStateHandle
. This would eliminate the above side effects while maintaining API consistency withLiveData
.My proposal is to expose
StateFlow
asMutableStateFlow
to match theLiveData
implementation, which would allow us to write more streamlined code:For context: Although the example is a simplified version of real-world scenarios, the struggle comes from using the new API in a real (closed source) codebase. We ended up creating our own extension function to handle it better, but this requires us to maintain a synchronization mechanism between two
StateFlow
s, which is far from ideal.