简体   繁体   English

使用数组SWIFT从孩子那里接收Firebase快照

[英]Receiving a Firebase snapshot from a child with an array SWIFT

So I am currently trying to take data from my Firebase database and set it as its own variable, but the child for each chart is a specific date and time (yy.mm.dd.hms). 因此,我目前正在尝试从Firebase数据库中获取数据并将其设置为自己的变量,但是每个图表的子级都是特定的日期和时间(yy.mm.dd.hms)。 So i have an array storing all the dates I need, but i cant reference them when calling my snapshot. 因此,我有一个数组,用于存储我需要的所有日期,但是在调用快照时无法引用它们。 I've tried these two methods which throw the error "(child:) Must be a non-empty string and not contain '.' 我尝试了这两种方法,它们会引发错误“(child :)必须是非空字符串,并且不包含'。” '#' '$' '[' or ']''" '#''$''['或']''“

var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss   

ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath.row]).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

and

var postCollection = [170802120618, 170802101427]  //yy.mm.dd.hh.mm.ss  
let selection = self.postCollection[indexPath.row]
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

And the Database chart being roughly: 数据库图表大致如下:

FIR{
    users{
        uid{
            username: UserName
            Posts{
                170802120618{
                    First Name: first
                }
            }
        }
    }
}

Right. 对。 You want the child key to be an autogenerated hashvalue. 您希望子键是一个自动生成的哈希值。 You can create these by using childByAutoId() . 您可以使用childByAutoId()创建它们。 Also if I were you, I would just store that dates as string and parse those as needed. 另外,如果我是您,我只会将该日期存储为字符串,并根据需要解析它们。 Something below would be an example: 以下是一个示例:

Posts {
   -Kebfdajksthm {
      first_name: "first",
      post_date: "yymmddhhmmss"
   }
}

Try This 尝试这个

var post = [String]()
ref.observe(.value, with: { (snapshot) in
            for item in snapshot.children{
                self.post.append((item as AnyObject).key)
        }
    })

Then you print "post" and you will get ["170802120618", "170802101427"] 然后打印“ post” ,您将得到[“ 170802120618”,“ 170802101427”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM