React-native-firebase: There is a way to clear persistence cache?

Created on 23 Jun 2017  路  8Comments  路  Source: invertase/react-native-firebase

Hi. I'm having a problem when persistence is enabled.

I've sent some data to the firebase, which I can see on the console.
This data has an userID reference of the firebase user authenticated via facebook.
I have a several data there, but I can only get old data.

When I try to get this data through

firebaseApp.database().ref('my_path')
.orderByChild ('user')
.equalTo('user_id')
.once('value').then((snapshot) => console.log(snapshot.val()));

which was recently created always the response is null.

I did a test changing the user_id in user property in old data for the user_id used above and also I have get null.
And when I tried to get the user_id I was before, it came, even though it was changed to another user_id.

I was using version 1.1.2 and upgrade to version 2.0.0 but the problem persists.
What can I do to be able to access recently uploaded data?

Most helpful comment

@DavideValdo You need to set keepSynced before your requests:

import firebase from 'react-native-firebase';
import firebaseConfig from '../../config/firebase';

const firebaseApp = firebase.initializeApp(firebaseConfig);
const productsRef = firebaseApp.database().ref('products');

productsRef.keepSynced(true);

Then you use reference

const response = await productsRef.orderByChild('merchant_id').equalTo(_vendorID).once('value');
const products = response.val();

All 8 comments

Change the .once to .on

But I don't want to add a listener. I want to get the fresh data only once after I push it.

This is built in and intended firebase behaviour - the way .on works is it emits 2 events, firstly it emits whatever is locally persisted in the apps local cache and then the second time when it receives a response back from the server with fresh data. This helps keeps apps responsive during periods of poor or no network connectivity.

Noting the above therefore once will always just give you the first one of those emitted events - the locally persisted state. As @akshetpandey suggested - you need to use .on - then when you have the fresh data (the second event) unsubscribe from the ref by subsequently calling .off.

@Salakar Thank you. I know that. That's exactly why I asked my question.
If the application is online, I see no reason to get data from the cache.
Or at least if there was a push recently in that path, it could invalidate the cache to get the latest data in case the application is online.
But I solved my problem with keepSynced.

@lucianomlima Can you please share your code?
I have this and can't get keepSynced() to work. How can I apply keepSynced() to a orderByChild()/equalTo() combo?

products = await firebase.database().ref('products').orderByChild('merchant_id').equalTo(_vendorID).once('value');
products = products.val()

@DavideValdo You need to set keepSynced before your requests:

import firebase from 'react-native-firebase';
import firebaseConfig from '../../config/firebase';

const firebaseApp = firebase.initializeApp(firebaseConfig);
const productsRef = firebaseApp.database().ref('products');

productsRef.keepSynced(true);

Then you use reference

const response = await productsRef.orderByChild('merchant_id').equalTo(_vendorID).once('value');
const products = response.val();

@lucianomlima wouldn't keepSynced keep syncing the location in background?

Best,

@skleest I'm not sure.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hapo-hoangnh picture hapo-hoangnh  路  52Comments

neverlan picture neverlan  路  47Comments

jasan-s picture jasan-s  路  137Comments

TalEliel picture TalEliel  路  48Comments

abdullahizzuddiin picture abdullahizzuddiin  路  80Comments