[英]swift firebase iterate each snapshot value to update / set a value
Im trying to fetch all data from firebase database and update or set(if nil) each value differently. 我试图从firebase数据库中获取所有数据,并以不同的方式更新或设置(如果为零)每个值。 for example i have 5 users: 例如我有5个用户:
[1-jon likedby:jef, 2-jef likedby:nil, 3-jake, 4-mat, 5-taylor likedby:jon] [1-jon喜欢的人:jef,2-jef的人喜欢:nil,3-jake,4-mat,5-taylor的人喜欢:jon]
It should update/set each value differently (when button pressed update/set value and go to next value. 它应该以不同的方式更新/设置每个值(当按钮按下更新/设置值并转到下一个值时。
my code shows that i could iterate but i have no ideas on how to set each value. 我的代码表明我可以迭代,但是我不知道如何设置每个值。
or is it not possible in firebase snapshot ? 还是在Firebase快照中不可能? if so please advice/suggest on other or similar methods to solve the problem. 如果是这样,请就其他或类似方法提出建议/建议以解决问题。
Database.database().reference().child("posts").observeSingleEvent(of: .value, with: { snapshot in
print("##########")
var dictionary = snapshot.value as! [String:AnyObject]
for(_,value) in dictionary{
if let firstKey = snapshot.key as? String{
var likedby = value["Liked By"] as? String
let name = value["name"] as? String
let age = value["age"] as? String
let caption = value["caption"] as? String
let picURL = value["photoURL"] as? String
let gender = value["gender"] as? String
print("TTT")
print(name, "was liked by",likedby)
if likedby == nil || likedby != "Othman" {
print("****")
print(name)
A Firebase DataSnapshot
is read-only . Firebase DataSnapshot
是只读的 。 It's mostly used to read back data previously fetched from Firebase. 它主要用于读取以前从Firebase提取的数据。
You need to use DatabaseReference.setValue
method instead to update a given node. 您需要使用DatabaseReference.setValue
方法来更新给定节点。 As mentioned in the comments, the DataSnapshot.ref
property provides a handy way to get back a DatabaseReference
corresponding to a given snapshot. 如注释中所述, DataSnapshot.ref
属性提供了一种方便的方法来取回与给定快照相对应的DatabaseReference
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.