David Henry
David Henry

Reputation: 3046

Find Firebase Child from Array Values - xcode 8 / swift 3

Id like to get a snapshot with snapshot.key as the UID if the snapshot.value is "pending". Store that into an array. Then i'd like to loop through the array of UID and pull of all the details from .child("users") i.e. "email", "name" & "profileURL". Not sure what the best route is here. Do I store the snapshot into a dictionary then filter dictionary to "pending" or do this within the snapshot call itself?

friends & users JSON structure in my Firebase Database (as below;)

{
  "friends" : {
    "YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
      "Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
    },
    "Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
      "YPQYLtXnMbbmFugrJJPYe6rOIJg2" : "pending",
      "ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : "pending",
      "lNI9FxCErqMUNiW43yiDpkNoljg1" : "pending"
    },
    "ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
      "Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
    },
    "lNI9FxCErqMUNiW43yiDpkNoljg1" : {
      "Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
    }
  },
  "users" : {
    "YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
      "email" : "[email protected]",
      "name" : "Karen Alaedidibgghi Liangescu",
      "profileURL" : "someURL"
    },
    "Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
      "email" : "[email protected]",
      "name" : "Patricia Alaefhcbebjid Warmansen",
      "profileURL" : “someURL”
    },
    "ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
      "email" : "[email protected]",
      "name" : "Harry Alaeejdjagjga Greenestein",
      "profileURL" : "someURL"
    },
    "lNI9FxCErqMUNiW43yiDpkNoljg1" : {
      "email" : "[email protected]",
      "name" : "Maria Alaefehgadbdg Valtchanovsky",
      "profileURL" : "someURL"
    }
  }
}

Any help greatly appreciated. Cheers!

Upvotes: 1

Views: 450

Answers (1)

MarksCode
MarksCode

Reputation: 8576

Here's how you'd get started, once you have your parsed data you can use different methods to filter it which you can look up through different stackoverflow questions answered in the past.

let dbRef = FIRDatabase.database().reference.child("friends")
dbRef.observeSingleEvent(of: .value, with: { snapshot in
    if let data = snapshot.value as? [String: [String: String]] {
        for user in data {
            // You'll now have a dictionary with neatly formatted values you can filter
        }
    }
})

Upvotes: 1

Related Questions