Reading from database while offline

515 views
Skip to first unread message

Kasbolat Kumakhov

unread,
Jun 14, 2016, 10:19:08 AM6/14/16
to Firebase Google Group
Hi!

I'm using 9.0.2 version of database engine in my Android app.
A small test case.
While online i initialize the app (inlcuding FirebaseDatabase.getInstance().setPersistenceEnabled(true);) and read some high level nodes (just IDs).
After that, internet access is shutdown and i'm trying to read some data using these IDs.

The structure is somewhat like this:

idList
  id_1
  id_2
  id_3

dataList
  id_1
    data_1
    data_2
  id_2
    data_3
    data_4
  id_3
    data_5
    data_6

What i expect to see is something like a timeout, error, empty data or anything else.
What i do see is no callbacks after addListenerForSingleValueEvent using this path (i.e. "dataList/id_1")

Is there anything else i should do?
Tested on emulator (different android versions) and on real devices.

Michael Lehenbauer

unread,
Jun 15, 2016, 10:46:48 AM6/15/16
to Firebase Google Group
This is the expected behavior.  If you have data cached offline (from a previous listen while online), your listener will get that data.  Else, you won't get any callback until you come back online and the SDK is able to synchronize with the backend servers and file the listener.

Some apps just display a spinner while waiting for data, or combine it with a timeout that says "Sorry, data couldn't be fetched."  You can also monitor ".info/connected" as an extra signal as to whether you should expect to receive data or not.

Hope this helps,
-Michael



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/022b7b22-bd06-4b0d-8367-caf69ae35bf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kasbolat Kumakhov

unread,
Jun 16, 2016, 10:26:14 AM6/16/16
to Firebase Google Group
I see. This is a bit unexpected.
Thanks for the answer. Will change the app structure for this.

среда, 15 июня 2016 г., 17:46:48 UTC+3 пользователь Michael Lehenbauer написал:

Aritra Roy

unread,
Jun 17, 2016, 10:54:20 AM6/17/16
to Firebase Google Group
Thanks for this information. I was looking for this actually.

So, is it a good idea to sync all the data for a particular user in first app load and then offline read/write should work fine? And how to implement a "timeout" mechanism as you had mentioned earlier? Is there anything in-built in Firebase for this?

Frank van Puffelen

unread,
Jun 18, 2016, 7:02:02 PM6/18/16
to Firebase Google Group
There is no built-in feature to show a spinner while waiting for data. You'll have to implement that yourself.

     puf

Aritra Roy

unread,
Jun 19, 2016, 11:05:30 AM6/19/16
to Firebase Google Group
Thanks for the answer. But I was not asking how to implement a spinner. It is quite obvious that it is not in-built in Firebase.

I was curious to know if there is a "time-out" mechanism in Firebase? I will give you a scenario.

Suppose I have #setPersistence(true)  in my app, so I should be able to work with my database seamlessly while offline too. It works, but not in some scenarios.

If I start the app for he first time without INTERNET connection, so there is no data available offline and online, I fetch some data but do not get any callback and my app gets stuck.

There should be a callback or a mechanism to let us know if there is no data available at all, and INTERNET connection is required to proceed.

Hope you can help us understand this scenario better.

Thanks in advance.

Jacob Wenger

unread,
Jun 20, 2016, 2:41:43 PM6/20/16
to fireba...@googlegroups.com
Aritra,

There is no built-in timeout mechanism like you are asking about. But you can monitor connection state by listening at the .info/connected node. Docs and code samples can be found here.

Cheers,
Jacob

Reply all
Reply to author
Forward
0 new messages