Fixed
Status Update
Comments
il...@google.com <il...@google.com> #2
Thanks for reporting this and providing some code snippets! Additionally, if you have a project I would be able to clone that reproduces the issue it would definitely help as well!
[Deleted User] <[Deleted User]> #3
You can clone this:
Run the Main Activity, scroll to the end of the list: I join a screen of the issue.
mg...@google.com <mg...@google.com> #4
Thanks for providing a sample project! Looks like this is an issue with how we conflate item accesses when triggering prefetch.
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.