SQLiteNoob
SQLiteNoob

Reputation: 3022

Get parent of child in Firebase

I am having a problem retrieving the child key and parent key from a Firebase database.

I have a firebase database that looks like this:

- friendlies
    - 38UUDYtde4WF5AVcxcw6o9qaPBM2
        - GYtQbt3KX1RveVeBAHd08u0Pdm42: true
    - GYtQbt3KX1RveVeBAHd08u0Pdm42
        - 38UUDYtde4WF5AVcxcw6o9qaPBM2: true

I am successfully getting this data via:

var REF_FRIENDLIES = Database.database().reference().child("friendlies")
REF.FRIENDLIES.queryOrderedByKey().observeSingleEvent(of: .value, with: {
        snapshot in
        snapshot.children.forEach({
            (snapChild) in
            print("SnapChild: \(snapChild)")
            let child = snapChild as! DataSnapshot
            print("Child Key: \(child.key)")
            print("Child Parent Key: \(child.ref.parent?.key)")
        })
        print("Snapshot: \(snapshot)")
    })

Which has the following output:

SnapChild: Snap (38UUDYtde4WF5AVcxcw6o9qaPBM2) {
    GYtQbt3KX1RveVeBAHd08u0Pdm42 = 1;
}
Child Key: 38UUDYtde4WF5AVcxcw6o9qaPBM2
Child Parent Key: Optional("friendlies")


SnapChild: Snap (GYtQbt3KX1RveVeBAHd08u0Pdm42) {
    38UUDYtde4WF5AVcxcw6o9qaPBM2 = 1;
}
Child Key: GYtQbt3KX1RveVeBAHd08u0Pdm42
Child Parent Key: Optional("friendlies")


Snapshot: Snap (friendlies) {
    38UUDYtde4WF5AVcxcw6o9qaPBM2 =     {
        GYtQbt3KX1RveVeBAHd08u0Pdm42 = 1;
    };
    GYtQbt3KX1RveVeBAHd08u0Pdm42 =     {
        38UUDYtde4WF5AVcxcw6o9qaPBM2 = 1;
    };
}

The child key is working properly; the problem I'm having is retrieving the parent data. What I want to return is a child ID, as well as its holder/parent data. So, I want to return GYtQbt3KX1RveVeBAHd08u0Pdm42 as the child key, but retrieve 38UUDYtde4WF5AVcxcw6o9qaPBM2 as the parent key, but what returns instead as the parent is friendlies.

The snapshot clearly contains this, but I am at a loss at how to access it.

EDIT:

So child.ref.description() will return me the node I want, but at the end of my database's address:

https://my-project.firebaseio.com/friendlies/38UUDYtde4WF5AVcxcw6o9qaPBM2 and I can just replace the url part of the String, but this seems like a really bad practice.

Upvotes: 0

Views: 374

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

Since you're looping over snapshot.children, the key of the parent is always going to be snapshot.key.


What I think you're trying to do is traverse the two level of child nodes under friendlies. But for that you will need two loops:

var REF_FRIENDLIES = Database.database().reference().child("friendlies")
REF.FRIENDLIES.queryOrderedByKey().observeSingleEvent(of: .value, with: {
        snapshot in
        snapshot.children.forEach({ (snapChild) in
            let child = snapChild as! DataSnapshot
            child.children.forEach({ (snapGrandchild) in
                let grandchild = snapGrandchild as! DataSnapshot
                print("Grandchild Key: \(grandchild.key)")
                print("Grandchild Parent Key: \(child.key)")
            })
        })
    })

I might be missing some null handling in the code, but I hope the flow is clear.

Upvotes: 1

Related Questions