question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

incomprehensible flow collection

See original GitHub issue

While 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:

  1. 30 is not collected
  2. 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:open
  • Created a year ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
TZankecommented, Jul 26, 2022

After some hours trying to write a test, i can conclude the following:

1) This will not work, cause get() called inside OnSharedPreferenceChangeListener 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.

0reactions
tfcporciunculacommented, Jul 25, 2022

Thanks for the re-investigation!

So the listener event started by 30 will change to a 50 inside the flow. Then the flow continues to the collection and prints 50. Afterwards the delayed listener event of 50 triggers the flow again, getting the 50 and also collects a 50.

Yeah, this kind of behavior is still reasonable to me given what we’re doing there.

But, is this a bug?

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 ✌️

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found