0

I'm using react-native-firebase in my app. The problem i'm facing is how to handle the UI updates when user tries to push data when offline.

If the user is online we can use the on() method to get realtime updates but what to do when they are offline. We know that the pushed data is stored in the cache and pushed when user is online again. Can this cached data be used to do what i aim at achieving?

Here's the code i used to receive realtime updates:

var ref333 = firebase.database().ref(`/user-posts/${uid}/`)
ref333.on('value',function (snap) {
  var s = snap.val();
  console.log("NEW POSTS "+JSON.stringify(s))
})

The code i use to push the data.

var postData = { uid: uid, body: 'body', title: 'title', starCount: 0 };

// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
var ref222 = firebase.database().ref(`/posts/${newPostKey}`)
var ref333 = firebase.database().ref(`/user-posts/${uid}/${newPostKey}`)
ref222.push(postData, function (onComplete) {
  console.log("COMPLETED")
  ref333.push(postData,function (onComplete) {
    console.log("NEXT COMPLETED")
  }, function (error) {
    console.log("ERROR IN ",error)
  })
}, function (error) {
  console.log("error == "+error)
})
addu
  • 71
  • 1
  • 9

2 Answers2

0

The .on snspashot listener should be triggered even if in offline mode. According to the docs: https://firebase.google.com/docs/database/web/read-and-write

You can use the value event to read a static snapshot of the contents at a given path, as they existed at the time of the event. This method is triggered once when the listener is attached and again every time the data, including children, changes.

This should work in offline mode as well. If you are not receiving updates - something else is wrong.

Daniel Dimitrov
  • 1,848
  • 21
  • 35
0

This problem was solved by adding this lines of code to your native code:

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

addu
  • 71
  • 1
  • 9