incomprehensible flow collection
See original GitHub issueWhile writing a test for my implementation with FlowSharedPreferences
i discovered a unexpected behavior of the Flow:
Test Code:
@Test
fun testBug3() = runTest {
val tokenPreferences = getApplicationContext<MDEApplication>().getSharedPreferences("Token", Context.MODE_PRIVATE)
val tokenPreferencesFlow = FlowSharedPreferences(tokenPreferences).apply { clear() }
val accessTokenPreferenceFlow = tokenPreferencesFlow.getFloat("access_token", defaultValue = 0.5f)
launch {
Log.i("TESTX", "collection started!")
accessTokenPreferenceFlow.asFlow()
// .distinctUntilChanged()
.collectLatest {
Log.i("TESTX", "collected: $it")
}
}
launch {
Log.i("TESTX", "setAndCommit 20")
accessTokenPreferenceFlow.setAndCommit(20f)
Log.i("TESTX", "setAndCommit 30")
accessTokenPreferenceFlow.setAndCommit(30f)
Log.i("TESTX", "setAndCommit 50")
accessTokenPreferenceFlow.setAndCommit(50f)
}
}
}
Output is:
TEST: collection started!
TEST: setAndCommit 20
TEST: collected: 20.0
TEST: setAndCommit 30
TEST: setAndCommit 50
TEST: collected: 50.0
TEST: collected: 50.0
For me i see two problems:
- 30 is not collected
- 50 is collected twice
As far as i can see right know, the two issues belong together.
Why i need this fixed in any way
In my Test i need to have a start value collected on flow collection start (Value X). Then later on, a second value (Y) is set and needs to be processed by the collection also. But right now, the first value is never processed, instead the first collection is the second value (Y). The first value is skipped for some unknown reason like 30 in the example above.
Sometimes even worse:
setAndCommit X
collection started!
setAndCommit Y
collected: X
Y is never collected. Right know i really don’t know whats going on. 😃
Issue Analytics
- State:
- Created a year ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Incompressible flow - Wikipedia
refers to a flow in which the material density is constant within a fluid parcel—an infinitesimal volume that moves with the flow velocity....
Read more >What is Incompressible Flow? - Fluid Flow / CFD - SimScale
In fluid dynamics, incompressible flow refers to a flow in which the density remains constant in any fluid parcel, i.e. any infinitesimal ...
Read more >Incompressible Flow: Panton, Ronald L. - Amazon.com
Incompressible Flow, Fourth Edition is the ideal coursebook for classes in fluid dynamics offered in mechanical, aerospace, and chemical engineering programs.
Read more >Incompressible Flow - an overview | ScienceDirect Topics
In an incompressible flow the force applied to the fluid by a stationary surface can be calculated directly from the vorticity in the...
Read more >Equations of Compressible and Incompressible Flow in Fluid ...
Incompressible flow reduces the continuity equation for conservation of mass to a divergenceless equation, and this greatly simplifies the Navier-Stokes ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After some hours trying to write a test, i can conclude the following:
1) This will not work, cause
get()
called insideOnSharedPreferenceChangeListener
also does return a newer value then the event is originated from. The listener is just a little bit slow.2) This should not make any difference at all.
3 & 4) As you said Also
distinctUntilChanged()
could break any logic waiting for the same value. So very client related option.Thanks for the re-investigation!
Yeah, this kind of behavior is still reasonable to me given what we’re doing there.
I say nay! 😃
About 3, I would prefer to not add
distinctUntilChanged()
in the lib and leave that as an option for clients. About 1 and 2, I’m not super confident about these changes and I would prefer to only go for something different than what we have if we’re really able to confidently narrow the issue down to something we fully understand and can reproduce. I apologize for not being of much help here, but if at any point you feel some of these changes should really make their way into the library, feel free to open an MR with a test as well!I’ll leave the issue open for now and close it eventually if there’s no more activity ✌️