0

or closing the app? How can I acess that data?

As I understood, firebase realtime database stores data and when a change in data is produced, you can retrieve the modified data. The app detects an event sent by the fb realtime database, and then downloads the new data. That is useful only with newly added data, but let's say you close the app. ¿Do you have the data from previous session? Lets set an example, an app like whatsapp. New messages are sent by an user to fb rt database, then firebase sends an event, the app detects the event, then downloads new data and displays it. But what if you close and reopen the app? Is the message still being displayed?

If not, how do I access that data? Should I keep data on a sqlite database on my own?

//Edit: The way the app is intended to work is: After data is modified, download new data. When app is closed after data has been downloaded, I want that data(and all previous data the app has downloaded) to still be there, so I can display the data when reopening the app. When app is closed, or there is no internet, then the app waits until there is internet and app is opened to download the new data. The part I do not understand yet is: Does the data downloaded from the server when there is new data disappear from the app after you close the app? Or it does disappear forever from the app memory if you do not save it in your own local database?

Kazekum
  • 3
  • 5
  • If you close the app, you better stop synchronisation with firebase, and resynchronise when user reopen the app. I don't know if android supports well firebase synchronisation in background, but for performance reasons, i guess the best bet is to only stay synchronised with firebase when your app is open. When the app is in background, use firebase notifications to send useful notifications to the user. – diouze Nov 28 '18 at 17:21
  • I edited it. I meant after closing and opening it again. Sorry about the confusion. – Kazekum Nov 28 '18 at 19:15

2 Answers2

0

Do you have the data from previous session?

Absolutely. If you are using the following line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Your data will be available even if your devices goes offline.

But what if you close and reopen the app? Is the message still being displayed?

You get all the data from the "previous session".

When talking about listeners that are getting the data in realtime, please don't forget to remove them according to the life-cycle of your activity as explained in my answer from this post.

Here you can also find more informations about the case in which you don't remove the listener.

Edit:

When app is closed after data has been downloaded, I want that data(and all previous data the app has downloaded) to still be there

This will certainly happen.

When app is closed, or there is no internet, then the app waits until there is internet and app is opened to download the new data.

This is what is happening if you are enabling offline persistence, as explained above.

Does the data downloaded from the server when there is new data disappear from the app after you close the app?

No, it does not.

Or it does disappear forever from the app memory if you do not save it in your own local database?

Will not disappear. There is no need for any other databases.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Your answer helped me, but it did not entirely resolve my question. Thats why I cannot check it as accepted. Same as the above question. Will edit the question though so I can express better the answer I'll get. Did upvote you, but I dont have 15 karma yet, so I cannot. – Kazekum Nov 29 '18 at 21:00
  • Please see my updated answer where I have answered you all the other questions. – Alex Mamo Nov 30 '18 at 11:06
  • So please reconsider accepting my answer. I'd appreciate it. Thanks! – Alex Mamo Nov 30 '18 at 11:07
0

When your application is in background (closed), it can't use registered listeners to maintain the synchronisation of your local data with the online data on firebase database.

When the user reopen the application, the data is automatically resynchronised to get the latest data from your database. See : https://firebase.google.com/docs/database/android/offline-capabilities

If you want to notify something to your user when the app is closed (like a new message or something), the best bet is to use Firebase Cloud Messaging : you can send notification messages that are displayed to your user (even if app is closed), or send data messages and determine completely what happens in your application code.

diouze
  • 532
  • 2
  • 11
  • What i meant is, do you get only the new data or the full data, when reopening app? – Kazekum Nov 28 '18 at 20:59
  • Your references will be updated to reflect current state of database (event listeners will trigger as many time they have to be triggered, aka 5 times if 5 messages have been added during the time the app was closed), you'll have to resynchronise your views to match changes, like you do when you receive new data when app is open. – diouze Nov 28 '18 at 21:18