Swift Firebase Baca Kunci di Autokey
//https://stackoverflow.com/questions/53350869/swift-fetching-all-users-to-an-array
//MVC - MVVM
//First Step
struct User{
var id:String
var username:String?
var occupation:String?
var age:Int?
var bio:String?
var email:String?
}
//Second Step
let query = self.ref.child("Users").queryOrdered(byChild: "email")
query.observeSingleEvent(of: .value) {
(snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
let id = child.key
//optional Line for sub childs
//let key = child.childSnapshot(forPath: "SubChild")//optional
let value = child.value as? NSDictionary//let value = key.value as? NSDictionary
let username = value?["Username"] as? String
let occupation = value?["Occupation"] as? String
let age = value?["Age"] as? Int
let bio = value?["Bio"] as? String
let email = value?["email"] as? String
let user = User(id: id, username: username, occupation: occupation, age: age, bio: bio, email: email)
self.usersArray.append(user)
}
completion("Users list fetched")
}
Lucky Lapwing