Koh
Koh

Reputation: 2897

Swift Firebase: Querying Child Nodes without Parent Key

Ok i have the following structure and I wanna pull out all the nodes which contains the registered info.

{
  "events" : {
    "hsoigjpoirt94pwjfjoijfdg" : {
      "coverImageURL" : "http://www.who.int/about/Logo-WHO.jpg",
      "dateTime" : "22 May, 2pm",
      "eventDescription" : "Lorem ipsum ...",
      "eventID" : "hsoigjpoirt94pwjfjoijfdg",
      "lat" : 1.2945,
      "lon" : 103.8822,
      "registered" : {
        "NJqoJ4iMTyXGGqfKADoKDLhDYQj1" : true
      },
      "title" : "Volunteer at WHO"
    },
    "hvasdhpoifah98whfaksad" : {
      "coverImageURL" : "http://s3.amazonaws.com/patientslikeme/organizations/11/NKF_D_NEWE_RGB-original.png?1317242994",
      "dateTime" : "24 July, 2:30pm",
      "eventDescription" : "Lorem ipsum ...",
      "eventID" : "hvasdhpoifah98whfaksad",
      "lat" : 1.432079,
      "lon" : 103.836871,
      "registered" : {
        "NJqoJ4iMTyXGGqfKADoKDLhDYQj1" : true
      },
      "title" : "Help at Yishun Dialysis"
    },
    "jioasdifueivaf5262d" : {
      "coverImageURL" : "http://www.publichygienecouncil.sg/images/default-source/Photo-Galleries/walk-for-your-kidneys-by-nkf/nkf-litter-picking_25-jan-2014.jpg?sfvrsn=6",
      "dateTime" : "12 Feb, 3-6pm",
      "eventDescription" : "Lorem ipsum  ...",
      "eventID" : "jioasdifueivaf5262d",
      "lat" : 1.33831,
      "lon" : 103.705326,
      "title" : "Charity Concert at NKF"
    }
  },
}

I wanna extract all the snapshots that consist of "registered" with the key "NJqoJ4iMTyXGGqfKADoKDLhDYQj1" : true . So far I attempted with the following but it pulls out everything:

ref.child("events").queryOrdered(byChild: "registered").observe(.value, with: { (snapshot) in

            let snapValues = snapshot.value as! [String: AnyObject]

            print(snapValues)                

        }) { (error) in
            print(error.localizedDescription)
        }

FYI, each of these nodes are events where individuals can register to the events. I wanna create view which lists all events a particular user has registered.

Upvotes: 1

Views: 841

Answers (1)

Koh
Koh

Reputation: 2897

Right, just when I was about to give up, I managed to find a solution to get the snaps I want. Here's how I go about doing it:

ref.child("events").queryOrdered(byChild: "registered").observe(.value, with: { (snapshot) in

            for snap in snapshot.children {
                let snapDataSnapshot = snap as! FIRDataSnapshot
                let snapValues = snapDataSnapshot.value as? [String: AnyObject]

                if let snapWithReg = snapValues?["registered"] as? [String: Bool] {

                    if snapWithReg[userUID]! {
                        print(snap)
                    }
                }
            }


        }) { (error) in
            print(error.localizedDescription)

        }

The snaps are all snaps with the 'registered' node and the userUID "NJqoJ4iMTyXGGqfKADoKDLhDYQj1" : true

Upvotes: 1

Related Questions