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:
Added this to my application delegate, right after FIRApp.configure():
FIRDatabase.database().persistenceEnabled = true
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.