从 Firebase 打印所有 Children 列 - iOS Swift 4
Print All Children Columns from Firebase - iOS Swift 4
我的用户有2条记录table
下面这段代码
let fcmTokenRef = Database.database().reference().root.child("users").child(id!).child("fcmToken")
fcmTokenRef.observe(DataEventType.value, with: { (snapshot) in
print(">>",snapshot)
})
会打印出child
的token
如何调整我的代码以打印 all 我所有 children 的代币?
您请求的是一次性读取,因此您只读取了一次数据。您需要使用 .childAdded
试试这个:
let fcmTokenRef = Database.database().reference().child(“users”)
fcmTokenRef.observe(.childAdded, with: { (snapshot) in
print(">>",snapshot)
guard let data = snapshot as? NSDictionary else {return}
var each_token = data[“fcmToken”] as? String
print(“all tokens: \(each_token!)”)
})
@puf 说了一些非常重要的话:
The child_added event fires for each matching child under the node that you query. If there are no matching children, it will not fire.
你可以试试
let fcmTokenRef = Database.database().reference().root.child("users").observe(DataEventType.value, with: { (snapshot) in
print(">>",snapshot)
let dic = snapshot.value as! [String:[String:Any]]
Array(dic.values).forEach {
let str = [=10=]["fcmToken"] as! String
print(str)
}
})
我的用户有2条记录table
下面这段代码
let fcmTokenRef = Database.database().reference().root.child("users").child(id!).child("fcmToken")
fcmTokenRef.observe(DataEventType.value, with: { (snapshot) in
print(">>",snapshot)
})
会打印出child
的token如何调整我的代码以打印 all 我所有 children 的代币?
您请求的是一次性读取,因此您只读取了一次数据。您需要使用 .childAdded
试试这个:
let fcmTokenRef = Database.database().reference().child(“users”)
fcmTokenRef.observe(.childAdded, with: { (snapshot) in
print(">>",snapshot)
guard let data = snapshot as? NSDictionary else {return}
var each_token = data[“fcmToken”] as? String
print(“all tokens: \(each_token!)”)
})
@puf 说了一些非常重要的话:
The child_added event fires for each matching child under the node that you query. If there are no matching children, it will not fire.
你可以试试
let fcmTokenRef = Database.database().reference().root.child("users").observe(DataEventType.value, with: { (snapshot) in
print(">>",snapshot)
let dic = snapshot.value as! [String:[String:Any]]
Array(dic.values).forEach {
let str = [=10=]["fcmToken"] as! String
print(str)
}
})