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,