0

Recently, I realized my app was plagued with some issues due to using addSingleValueEventListener while I have setPersistence equal to true. keepSynced(true) has been nothing but trouble and doesn't solve the problems I've encounters.

Here's an example of something I do in many different places that is causing the trouble:

onClick: createPost(userId)

createPost(userId) {
    myRef.child("banned_users").child(userId).addListenerForSingleValueEvent() {
        if(datasnapshot.exists()) {
            // do nothing
        } else {
            completeCreatePost()
        }
    }
}

From my understanding, such an operation would get the cached copy of this data on the first attempt. To fix this, it is recommended to use a normal value event listener.

From my understanding, the reason to use a non-single value event listener with persistence set to true is because the first attempt will get the cached data and then it will trigger again when it gets the database data. But I'm not sure how, or if it's even possible, to stop listening for that data once I've received the non-cached version of it.

Does anybody know how I can use persistence for caching AND use single value event listeners with a guarantee of getting the most recent data? Everything I've tried so for turns out to be very hackey and leads to undesired behavior,

Haidar Hammoud
  • 83
  • 3
  • 12
  • Sorry, but it's very hard to understand what you're asking through the amount of text. I recommend reducing this considerable to improve the chance we can answer. You already seem to know that [disk persistence and single-value event listeners don't work well together](https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent/34487195#34487195), because you may end up reading a state value. If your list of banned used gets updated during the lifetime of the app, you should keep the app's version of it up to date with a regular event listener. – Frank van Puffelen Jan 21 '19 at 04:57
  • Alright, I made it more concise, hopefully it's clear now. My problem with using a regular event listener is that the functions will be triggered every single time the data changes. This is extremely problematic in many situations as the functions relying on the data from those listeners might cause repeat events on callbacks and also might trigger a circular execution of events in a few cases. – Haidar Hammoud Jan 21 '19 at 06:07
  • 1
    "Does anybody know how I can use persistence for caching AND use single value event listeners with a guarantee of getting the most recent data?" This isn't possible. The answer I linked explains why. You can get close by calling `keepSynced(true)` on the reference, but there's no guarantee you'll never see stale data that way – Frank van Puffelen Jan 21 '19 at 15:33

1 Answers1

0

As Frank said in the comments, this isn't possible This post explains why, but has also been updated to include a fix to this issue.

The post explains a new method that allows you to get the most recent data while only relying on cacheing when connection fails.

Haidar Hammoud
  • 83
  • 3
  • 12