0

lets say we have a button called "AddItemButton", so that each time we click the button we should add a push id along with a certain value in the database at (Items) node.

looks like this:

//on click AddItemButton

DatabaseReference items=FirebaseDatabase.getInstance().getReference();

//push an item 

items.child("Items").push();

//add to database

items.setValue("new_item").addOnCompleteListener(new onCompleteListener..{

  //when completed the write add data to (Items_2) node

   DatabaseReference items_another=FirebaseDatabase.getInstance().getReference();

   items_another.child("Items_2").push();

   items_another.setValue("new_item");


});

So as you see its a basic way to add data to (Items) and once completed another write is added to (Items_2).

Going Offline now

If I go offline now and click the button 2 times. I know that I cached 2 writes to my disk (because I enabled persistance);

Going Online now

If I go now online the 2 writes of (Items) is added, while at (Items_2) nothing is added (they are lost).

Question 1

Does firebase also allow to catch Completion Listener associated which each write offline?

If yes then, why its not persisted in my case?

Question 2

Do we have a limit for cached writes?

Thank you.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • 1
    Completion listeners are not persisted across app restarts. – Frank van Puffelen Feb 07 '18 at 22:00
  • @FrankvanPuffelen you mean they are lost at the moment I exit the app? – Hasan Bou Taam Feb 07 '18 at 22:02
  • @FrankvanPuffelen what do you recommend to make sure I execute the completion listener offline (for each write)? – Hasan Bou Taam Feb 07 '18 at 22:04
  • A bit more in the behavior: https://stackoverflow.com/questions/32725075/firebase-android-how-to-tell-if-node-has-been-synced. – Frank van Puffelen Feb 07 '18 at 22:28
  • One thing your could do is do a "gate write" when the app starts. Since writes from an single app are executed in the order in which you performed them, once the "gate write" is completed, you can be sure the others have executed too. – Frank van Puffelen Feb 07 '18 at 22:29
  • @FrankvanPuffelen (gate write == to any random write to the database), so you mean once the app starts again I write a random database trigger in some node. And that will push everything from cache? – Hasan Bou Taam Feb 07 '18 at 23:21
  • The queued writes will automatically be sent to the server once there is a connection. You don't need to do anything for that. But if you want to know when those pending writes have been committed (and your existing completion listeners have been lost), you can do one additional write that you use as a detection mechanism. – Frank van Puffelen Feb 07 '18 at 23:22
  • @FrankvanPuffelen in other words, this means that firebase is actually persisting the Completion listener, but it won't release it after app restarts? – Hasan Bou Taam Feb 07 '18 at 23:23
  • @FrankvanPuffelen its a bit tricky to understand. Lets say (A) is a write and (B) is the completion of it, and also (C) is a write and (D) is the completion for it. Then if I triggered offline and then waited until online then {A && B} will go to the database but {C && D} will stay in cache and will be lost. Then how come a (gate write) will make (C && D) go to the database? Thank you for your reply. – Hasan Bou Taam Feb 07 '18 at 23:29
  • @FrankvanPuffelen or {C && D} will be lost and would never reach the database? – Hasan Bou Taam Feb 07 '18 at 23:36
  • Firebase completion listeners do not persist across app restarts. You'll have to detect the state upon restart, for example with the trick I gave. – Frank van Puffelen Feb 08 '18 at 00:57
  • @FrankvanPuffelen I totally understand what you are saying, and its true but I don't think you got my question right or I didnt explain it well enough. – Hasan Bou Taam Feb 08 '18 at 01:31
  • @FrankvanPuffelen If completion listeners are not persisted across app restarts, what happens to the other writes that I may place in the completion listener (((ARE THEY LOST TOO))? If yes what can I do about it? – Hasan Bou Taam Feb 08 '18 at 01:34

0 Answers0