5

I'm trying to add offline capabilities to our iOS app (simply put, an app that manages users and projects), which relies on Firebase. This is using Swift 3.0. I have followed the guides and did the following:

  1. Added this to my application delegate, right after FIRApp.configure():

    FIRDatabase.database().persistenceEnabled = true

  2. called keepSynced(true) on users/userKey and all of said user's projects/projectKey nodes.

The app works fine while online (obviously) and keeps working equally well offline, even if I restart it while disconnected from the internet. The problem arises when I create a new project while offline. I use the following to create a new project:

let projectKey = FIRDatabase.database().reference(withPath: "projects").childByAutoId().key
let logsKey = FIRDatabase.database().reference(withPath: "projects").child(projectKey).child("logs").childByAutoId().key
FIRDatabase.database().reference().updateChildValues([
    "projects/\(projectKey)/key1" : value1,
    "projects/\(projectKey)/key2" : [
        "subkey1" : subvalue1,
        "subkey2" : subvalue2
    ],
    "projects/\(projectKey)/key3/\(logKey)" : [
        "subkey3" : subvalue3,
        "subkey4" : subvalue4
    ]
]) { error, ref in
    if error != nil {
        print("Error")
        return
    }
}

After the project's creation, if I attempt to call observeSingleEvent on "projects/projectKey/key1" or "projects/projectKey/key2", all is good. However, calling the same function on "projects/projectKey/key3/logKey" never triggers the block/callback - it only gets called if the connection comes back.

I have enabled the FIRDatabase logging (which confirms the local write takes place) to look for hints but can't seem to figure out the issue.

Is there something I'm missing?

Note: Using the latest Firebase iOS SDK (3.5.2).

EDIT: It seems to work fine if I expand the deep links:

let projectKey = FIRDatabase.database().reference(withPath: "projects").childByAutoId().key
let logsKey = FIRDatabase.database().reference(withPath: "projects").child(projectKey).child("logs").childByAutoId().key
FIRDatabase.database().reference().updateChildValues([
    "projects/\(projectKey)" : [
        "key1" : value1,
        "key2" : [
            "subkey1" : subvalue1,
            "subkey2" : subvalue2
        ],
        "key3" : [
            logKey : [
                "subkey3" : subvalue3,
                "subkey4" : subvalue4
            ]
        ]
    ]
]) { error, ref in
    if error != nil {
        print("Error")
        return
    }
}

Could this be a bug in the way Firebase manages its local cache states when offline? It's as if it didn't know about intermediate keys created using deep links in updateChildValues.

bbousquet
  • 49
  • 1
  • 4
  • This could be a bug... A good indicator would be if the problem goes away if you change the code to just do multiple .setValue calls instead of .updateChildValues. In any case, can you report it to support with full repro code (including the observer that doesn't fire)? https://firebase.google.com/support/contact/bugs-features/ Also, be aware that your second code snippet (after the EDIT) will overwrite all of projects/\(projectKey) and delete whatever data was there, which may not be what you were intending. – Michael Lehenbauer Sep 17 '16 at 00:47
  • Did you get anywhere with the bug report? I'm experiencing similar issues. – Michael Waterfall Mar 18 '17 at 17:09
  • Not really, my workaround was to expand the deep links accordingly. This is annoying but seems to be par for the course with Firebase (personal opinion based on how slow they are moving, SDK wise - bug fixes, platform support, etc.). – bbousquet Mar 19 '17 at 13:39

0 Answers0