Git Product home page Git Product logo

Comments (24)

Salakar avatar Salakar commented on May 17, 2024 3

@Leeds-eBooks afaik .keepSynced() prevents it from being evicted out the persistent disk cache, the disk cache has a maximum capacity of 10mb, if that's reached evictions of stale/least used data is done. Without it but with persistence enabled it still will work - unless the cache is full and it gets evicted. And of course it will keep the data fresh, whereas without it persisted data is only updated when accessed via a ref.

from react-native-firebase.

bakr-baroudi avatar bakr-baroudi commented on May 17, 2024 1

@dzuncoi You need to add an event Listener to your path data.

  • In this event I am listening to adding data:
listenForAddingItems() {
		this.userRef.on('child_added',  (snap) => {
			console.log(snap)
			this.items.push({ id: snap.key, ...snap.val()});
			this.setState({
				dataSource: this.state.dataSource.cloneWithRows(this.items)
			});
		});
  } 

please check this

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024 1

Oh man, you made my day @bakr-baroudi. Never think of I should add listener to my path. Thank you so much

from react-native-firebase.

victorkvarghese avatar victorkvarghese commented on May 17, 2024 1

For anyone looking to enable offline persistence in rn-firebase

Enabling database persistence (setPersistence) via JS for the default app is no longer supported. This breaking change was made in v3.0.0 to avoid race condition issues. You can still however easily enable this natively for the default app instance:

https://rnfirebase.io/docs/v4.0.x/core/default-app#Enable-Database-Persistence

Android
Add FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside your MainApplication.java files onCreate() method.

You also need to explicitly add the FirebaseDatabase import with the rest of the imports near the top of your MainApplication.java file:

import com.google.firebase.database.FirebaseDatabase;COPY

iOS
Add [FIRDatabase database].persistenceEnabled = YES; after the [FIRApp configure]; line inside your AppDelegate.m files didFinishLaunchingWithOptions method.

from react-native-firebase.

Ehesp avatar Ehesp commented on May 17, 2024

Have you tried this?

const ref = firebase.database().ref('my_ref');
ref.keepSynced(true);

ref.set(myData).then().catch();

from react-native-firebase.

benadamstyles avatar benadamstyles commented on May 17, 2024

I was struggling to work our what .keepSynced() actually does but I just found this.

So in other words, is it true to say that offline won't really be useful unless we use .keepSynced()?

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

I've tried . keepSynced() but it doesn't work @Ehesp, same behavior.

from react-native-firebase.

Ehesp avatar Ehesp commented on May 17, 2024

Okay, what about when you initilize the module?

Firebase.initializeApp({
  debug: __DEV__ ? '*' : false,
  errorOnMissingPlayServices: false,
  persistence: true, // <<<<<<<<<<<<<<<<<<<<
});

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

I initialize as setup doc:

import RNFirebase from 'react-native-firebase'

// Config
const firebase = new RNFirebase({
  debug: true,
  persistence: true
});

export default firebase

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

@Salakar so you mean whenever app is opened, we must check firebase local database and update un-updated records?

from react-native-firebase.

Ehesp avatar Ehesp commented on May 17, 2024

I believe internally in our app we use redux-persist to keep the data from firebase in redux. Even offline redux gets updated, so when the app boots we just re-sync it back up with firebase.

Its probably worth checking, somehow, whether the native SDKs will sync on app reboot. If they do, then there's an issue we need to sort.

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

From Firebase doc

When persistence is enabled, this queue is also persisted to disk so all of your writes are available when the user or operating system restarts the app.

I do believe Firebase native SDK can handle syncing offline data after app is shut down and reopened. Can you make a quick check about this issue? Or do you know how to check firebase un-synced local records? I'll need this way to fix this issue temporarily.

from react-native-firebase.

Salakar avatar Salakar commented on May 17, 2024

@dzuncoi is this on android or ios that you're having a problem with?

Can you try in your main application constructor call:

firebase.database().setPersistence(true);

If its android then i think it might be a race condition relating to https://github.com/invertase/react-native-firebase/blob/master/docs/firebase-setup.md

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

@Salakar I'm having problem on iOS, both simulator and device.
Hmm, I cannot use firebase.database().setPersistence(true); because setPersistence() is not a function and I don't find setPersistence method inside database class.

from react-native-firebase.

bakr-baroudi avatar bakr-baroudi commented on May 17, 2024

@dzuncoi Have you tried to reinstall the app after initializing the firebase?
Its working fine for me even if I restarted the App.

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

@bakr-baroudi yes, I tried to reinstall everything and it still doesn't work correctly. Did you completely shut down app and restart, then Firebase automatically sync data for you?

I don't know if I'm missing something but after my app is restarted, I need to execute a request and Firebase, then, will sync all un-updated data for me. Is that a correct behavior or Firebase should automatically do that work?

from react-native-firebase.

bakr-baroudi avatar bakr-baroudi commented on May 17, 2024

yes @dzuncoi, Its working for me.

  • How I tested this use case:
    1- Switch to offline mode.
    2- Add some data to my app.
    3- Close the App.
    4- Connect to Network.
    5- Launch the App wait for few seconds. (Syncing)
    6- Data is available in both remote and local databse.

from react-native-firebase.

dzuncoi avatar dzuncoi commented on May 17, 2024

Hi @bakr-baroudi, at step 3, Close the App., do you let it run in the background, or totally kill it (for ex on iOS: double tab Home > swipe App thumbnail away)? Your approach also works for me if I don't kill the app

from react-native-firebase.

bakr-baroudi avatar bakr-baroudi commented on May 17, 2024

@dzuncoi I totally close the App.

from react-native-firebase.

raduflp avatar raduflp commented on May 17, 2024

I'm getting the same issue on Android using [email protected]
After pushing new data offline the state is persisted, if I kill the app and restart it, I still can see the data. But as soon as I get the app online looks like the sync process deletes the data entered offline. Tried .keepSynced(true) to no avail.
Looks like killing the app messes up the ability of the sync process to reconcile the online and local state.

Thinking of implementing something similar to what @Ehesp described but syncing data manually opens up another area for errors and unforeseen scenarios.
Would upgrading to 3.1.x help?
Any suggestions would be appreciated

EDIT:
Looks like upgrading to 3.1.0 solved the issue for me

from react-native-firebase.

CharlieHawker avatar CharlieHawker commented on May 17, 2024

@raduflp can you confirm 3.1.0 definitely works for you? I'm seeing no persistence of data submitted when offline and app closed (killed) or device restarted before re-connecting, even if I re-trigger a push to the same refs after I restart the app...

from react-native-firebase.

raduflp avatar raduflp commented on May 17, 2024

It fixed the issue on Android, but in my app I still have this issue on iOS Release build only.
At the same time I could not reproduce the issue using the example app, so it must be a configuration issue with my project or maybe some weird incompatibilities with other packages when the code is minified.

I'll investigate a bit more in the next couple of days and I'll get back to you

from react-native-firebase.

kalai092 avatar kalai092 commented on May 17, 2024

@bakr-baroudi, @dzuncoi

can you please explain bit more on the below code, how it will work if i totally close the my app? for example in offline mode am trying to push some data into firebase using .push() and closed my app, so will the data get push while reopen my app with online mode?

am using firebase from 'react-native-firebase';

listenForAddingItems() {
this.userRef.on('child_added', (snap) => {
console.log(snap)
this.items.push({ id: snap.key, ...snap.val()});
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.items)
});
});
}

from react-native-firebase.

SourceCipher avatar SourceCipher commented on May 17, 2024

I tried hundreds of ways to get the react native to work offline but it never does.. The problem is with the cache management.. On native android it will always load everything from the cache if exists and then updates the data after looking at the db, on react native it never happens like that.. It always will show no data and then either loads from cache or db..

from react-native-firebase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.