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.

Flow not updating on SharedPreferences value change

See original GitHub issue

I’m having an issue where after the initial value my Flows aren’t updating when a new value is saved in SharedPreferences.

I’ve linked to a project that reproduces it but the gist is:

       val myPref: Preference<Int> = preferences.getInt(MY_PREF_KEY)

        update_pref.setOnClickListener {
            val selection = (0..2).random()

            GlobalScope.launch { myPref.setAndCommit(selection) }
        }

        myPref.asFlow()
            // This doesn't get triggered after the initial value
            .onEach { Log.d(tag, "PREF CHANGE: $it") }
            .launchIn(GlobalScope)

Simplified project to reproduce.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
davidbilikcommented, Aug 17, 2020

I also had this problem and I’ve traced that it’s connected to the “problem” that my coroutine was probably garbage collected since SharedPreferences does not store strong reference to the registered SharedPreferences.OnSharedPreferenceChangeListener. My code was something like

class MyClass {

    init {
        GlobalScope.launch {
            flowPreferences.getBoolean("my_prop")
                .asFlow()
                .collect {
                    ...
                }
        }
    }
}

I’ve fixed that by storing given Job to the field property

class MyClass {

    val observingJob: Job

    init {
        observingJob = GlobalScope.launch {
            flowPreferences.getBoolean("my_prop")
                .asFlow()
                .collect {
                    ...
                }
        }
    }
}

it’s kind of hack/workaround but it helped

0reactions
tfcporciunculacommented, Feb 18, 2021

Good findings here, but I’m closing this issue as I don’t think this is really related to the library itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Preferences DataStore Flow Doesn't Emit Same Value
Each time you want to update data, you first compare the new value with the previous one. And if data is the same,...
Read more >
SharedPreferences.OnSharedPreferenceChangeListener
Called when a shared preference is changed, added, or removed. This may be called even if a preference is set to its existing...
Read more >
All about Preferences DataStore - Medium
In this post, we will take a look at Preferences DataStore, one of two DataStore implementations. We will go over how to create...
Read more >
Is Jetpack DataStore a replacement for SharedPreferences?
It was built using Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally. There are two ways of ...
Read more >
Listening for preference value change | Android Studio
SharedPreferences Change Listener Android | Listening for preference ... https://youtu.be/TsASX0ZK9ak ◀️ Delete shared preferences data ...
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