0

I am trying to run a query on the firebase database and if the user is offline I want to display a pop up to the user prompting them to try again. However the query does not return when offline.

databaseRef.queryEqual(toValue: "someValue").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
    print(snapshot) //this is never called when user is offline
}) { (error) in
    print(error)    //neither is this
}

What is the best strategy to run this query and detect if the user was offline?

Nilsymbol
  • 515
  • 1
  • 9
  • 25

1 Answers1

1

Since the database client is not connected to its backend and it doesn't have any knowledge of the location in its cache, it cannot invoke your callback.

You can detect whether the Firebase client is connected to its backend by observing .info/connected. See the Firebase documentation for an example of detecting connection state.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Puf! What would you recommend as a best (safest) approach. 1) Just as in your answer, listning to wether the firebase client is connected and disallowing a query if not connected, or 2) Try the query and if the query doesn't return within X seconds then consider it failed and remove the observer? – Nilsymbol Mar 04 '17 at 12:40
  • I'd never work with time-outs unless I have a need for something time-based. That's not the case here. If the location is completely new and created in the client, consider priming the location by calling `setValue(nil)` on it. That way the client knows that there is no value and will fire. I recently answer a very similar question here: http://stackoverflow.com/questions/42430992/firebase-ondatachange-not-called-when-offline/42431732#42431732 – Frank van Puffelen Mar 04 '17 at 16:06