I've read about a hundred posts on here about dealing with the value of "childByAutoId" children from Firebase's Realtime Database... but I haven't exactly found anything that would explain what I'm trying to do so I figured I'd finally break down and ask.
First off here's the database structure:
let database = Database.database().reference(withPath: "messages")
let db1 = database.child("sender").child("receiver").childByAutoId()
Pretty straightforward. I then wanted to retrieve the value of that autoID.
db1.observeSingleEvent(of: .value, with: { snapshot in
guard let value = snapshot.value as? [String: Any] else{
completion(.failure(DatabaseError.failedToFetch))
print("GetAll Failed")
return
}
...returns the "failedToFetch" error, while:
database.child("sender").child("receiver").observeSingleEvent(of: .value, with: { snapshot in
guard let value = snapshot.value as? [String: Any] else{
completion(.failure(DatabaseError.failedToFetch))
print("GetAll Failed")
return
}
...which is the same thing only excluding childByAutoId returns:
"-MrdAxlUKHvJWjtSQe7X": {
body = "cookies";
createdat = 1640294767943;
msgId = "-MrdAxlUKHvJWjtSQe7X";
name = glynis;
receiverUid = LKJHdhkjhkjsh;
senderUid = LAKSjksljlkajlk;
}
So now the data is coming in... but when I try to get the value of "-MrdAxlUKHvJWjtSQe7X" (the auto-generated key):
let things: [Thing] = value.compactMap({ dictionary in
guard let name = value["name"] as? String,
let msgId = value["msgId"] as? String,
let body = value["body"] as? String,
let receiverUid = value["receiverUid"] as? String,
let senderUid = value["senderUid"] as? String,
let createdat = value["createdat"] as? String,
let dated = value["dated"] as? String,)else {
return nil
}
And I do a:
guard !things.isEmpty else {
print("thing are empty")
return
}
They come up empty (even though "value" is certainly populated.) So my question is how would I properly retrieve the value of the generated key (childByAutoId)?