1

I have a messaging app and I'm using firebase Offline, When I updatechildvalues, it goes to the local cache and then to the server. However, when i turn off my internet, it only goes to the local cache, and if i close the app, the data is lost.

is there a way to determine if something was saved to the server instead of the local cache?

if not, what is the most effective way, to determine if something was successfully sent to the server.

I tried an observe value, but it also observes offline updates.

slimboy
  • 1,633
  • 2
  • 22
  • 45
  • 1
    You'd need to attach a completion listener to detect when the write has been committed to the database server. What platform are you coding for? – Frank van Puffelen Feb 07 '17 at 14:54
  • Hi I'm coding on swift, and I'm using firebase offline. When I attached firebase offline, i'm never able to detect if it saved on the server. sorry for the late response. – slimboy Feb 10 '17 at 08:18
  • since it automatically saves in the offline cache, and the childadded gets triggered from that, however i need a way to know if it succesfully reached the online database, thank you for your help – slimboy Feb 10 '17 at 08:44
  • 1
    A completion listener will fire only when the data is written to the server. See an example here: http://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database/23294/detect-errors-when-writing-a-value-on-ios#t=201702101547129757355. But if there's been an app restart in between the write operation and the commit to server, that callback won't be fired. There is no workaround for that at the moment. – Frank van Puffelen Feb 10 '17 at 15:47
  • it worked, thank you, you can put it as an answer whenever you like! – slimboy Feb 11 '17 at 00:22
  • I wish i was @FrankvanPuffelen on this answer :), Firebase Rocks guys. – Aragunz Feb 11 '17 at 00:39

2 Answers2

2

A completion listener will fire only when the data is written to the server. See an example:

let message = ["name": "puf", "text": "Hello from iOS"]
ref!.childByAutoId().setValue(message) { (error) in
    print("Error while writing message \(error)")
}

But if there's been an app restart in between the write operation and the commit to server, that callback won't be fired. There is no workaround for that at the moment.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I am finding myself in about the same predicament as the OP. Could you please point me in the right direction with this question. https://stackoverflow.com/questions/45266392 – bibscy Jul 23 '17 at 21:46
1

I had the same problem, and it can be solved using cloud function. Basically, just create a cloud function called when your message is created, and set a "persisted" field to true to this message. It will propagate to your client, who will treat any message without the "persisted" property as a message not sent yet.

exports.confirmMessageSave = functions.database
  .ref('/chat/messages/{channelId}/{messageId}')
  .onCreate(function(event) {

    var message = event.data.val();

    var updateFanOut = {};
    updateFanOut['/messages/' + event.params.channelId + '/' + event.params.messageId + '/persisted'] = true;
    return admin.database().ref('/chat').update(updateFanOut);
});
PhilippeAuriach
  • 2,418
  • 2
  • 22
  • 41