1

I'm using firebase with app started in offline mode, when i'm subscribing to child values of some node the callback from observe*(_:,withBlock:) is not firing (neither for initial values nor changes). Subscriptions to direct values (childless) works fine. Take a look at a snippet :

    let database = FIRDatabase.database()
    database.reference().keepSynced(true)
    let databaseRef = database.reference()
    database.goOffline()


    databaseRef.child("user").setValue("user1")
    let userKey = databaseRef.child("usr").childByAutoId().key
    let userValues = ["uid": "uid",
                      "name" : "name",
                      "surname" : "surname"]
    databaseRef.child("/usr/\(userKey)/").setValue(userValues)

    //1
    databaseRef.child("user").observeSingleEventOfType(.Value, withBlock:{ snap in
        print("works")
    })
    //2
    databaseRef.child("usr").observeSingleEventOfType(.Value, withBlock:{ snap in
        print("doesnt work")
    })
    //3
    databaseRef.child("usr/\(userKey)/uid").observeSingleEventOfType(.Value, withBlock:{ snap in
        print("works")
    })`

subscriptions 1 & 2 works fine, but subscribtion 2 won't fire, until at least once database go online. From the moment database syncronize with remote i can go offline and everything works as it should. Anyone know how to handle this issue?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mprostak
  • 21
  • 2

2 Answers2

3

When your app is offline, the Firebase client will fire events from its cache. If your app has never connected to the Firebase servers, this cache will be empty.

That means that the Firebase client has no knowledge on whether a value exists at the location you request. For that reason it will not fire an event.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for reply. If thats the case then why event is fired for case 1 & 2 in the snippet where i reference a single value instead a child node? – mprostak Jun 10 '16 at 07:59
  • Number 3 fires because you've set the value of that location in your own code. So the Firebase cache has a value for that node. I think the same applies to your first test, but am not sure. – Frank van Puffelen Jun 10 '16 at 14:51
  • For each case i'm setting a value - 1: value "user1" for node "user", for second i'm setting child node with values for node "usr", for 3rd i'm setting a single value in child node for node "usr". Still don't understand why for the 2nd one no even is fired. Please note that if I go at least once online, I can work in offline mode without issues, those 3 test fires normally - again, if I delete app and start fresh in offline, problem presits. – mprostak Jun 10 '16 at 15:52
  • @FrankvanPuffelen In my case, I'm showing a loading view before calling `observe` and hiding this view once I get into the closure. How can I then stop the loading view if the closure is never trigger? – Nico Feb 21 '17 at 12:02
  • I'm having this issue with updateChildValues - it is not firing for some reason. Sometimes it does, sometime it does not. There are no logs. Is there a way to detect if it thinks it is in offline mode and switch it – JamesG Sep 22 '17 at 10:17
-1

I had a similar issue but I was using deep links with updateChildValues, which somehow caused the local cache to not fire events on intermediate (/path/intermediate/otherpath) keys. The workaround I found was to be more verbose in the dictionary I passed to updateChildValues. (I still believe this is a bug in the Firebase SDK).

See this Stack Overflow question

Community
  • 1
  • 1
bbousquet
  • 49
  • 1
  • 4