【问题标题】:Firebase -How to loop through snapshot.hasChild("...")Firebase - 如何遍历 snapshot.hasChild("...")
【发布时间】:2019-09-04 08:02:57
【问题描述】:

我的数据库如下所示

users
  |
  @--uidABC
       |
       |---phone:"+13125550690"
       |
       |---followers
              |
              |----uid123
              |      |---timeStamp:111222
              |
              |----uid456
              |      |---timeStamp:777999

我使用.observeSingleEvent 检查uidABC 是否存在,如果存在,我想检查该路径下是否有一个名为followerschild

我可以使用snapshot.hasChild("followers") 来查看它是否可用以及是否可用如何循环遍历下面的所有子节点 snapshot.hasChild("...")

我使用下面的代码,但它循环了错误的snapshot。当它应该使用DataSnapshotfollowers 下的任何内容时,它会使用顶级一级

let ref = Database...child("users").child(uidABC)
ref.observeSingleEvent(of: .value, with: { (snapshot) in

    if !snapshot.exists() {

        // this user doesn't exist
        return
    }

    if !snapshot.hasChild("followers") {

       // this user exists but has no followers
       return
    }

    // this user exists and has followers now loop through them
    for uid in snapshot.children {

        let snapshot = uid as! DataSnapshot

        if let dict = snapshot.value as? [String:Any] {
            let timeStamp = dict["timeStamp"] as? Double
            // eg. check for a specific timeStamp
         }
     }
})

【问题讨论】:

    标签: ios swift loops firebase-realtime-database


    【解决方案1】:

    我从here 和这里得到了答案

    let ref = Database...child("users").child(uidABC)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
    
        if !snapshot.exists() {
    
            // this user doesn't exist
            return
        }
    
        if !snapshot.hasChild("followers") {
    
            // this user exists but has no followers
            return
        }
    
        // this user exists and has followers now loop through them
        let childSnapshot = snapshot.childSnapshot(forPath: "followers") {
    
        for child in childSnapshot.children {
    
            let snap = child as! DataSnapshot
    
            let uid = snap.key
    
            if let dict = snap.value as? [String: Any] {
    
                let timeStamp = dict["timeStamp"] as? Double ?? 0
                // eg. check for a specific timeStamp
            }
        }
    })
    

    【讨论】:

      【解决方案2】:

      使用child

      // this user exists and has followers now loop through them
      for uid in snapshot.childSnapshot("followers") {
      
          guard let snapshot = uid as! DataSnapshot else {
           // Followers error
          }
      
          if let dict = snapshot.value as? [String:Any] {
              let timeStamp = dict["timeStamp"] as? Double
              // eg. check for a specific timeStamp
           }
       }
      

      您可以在此处找到更多信息: Iterate over snapshot children in Firebase

      【讨论】:

      • 谢谢,我试过了,但出现错误 - 'DataSnapshot' 类型的值没有成员 'child';你的意思是“孩子”吗?
      • child 已重命名为 childSnapshot(forPath: ) 感谢您纠正我:) @LanceSamaria
      • 我什至不知道我在纠正你。我以为您正在关闭一个完全不同的快照子属性。感谢您的帮助???
      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2018-02-26
      • 1970-01-01
      • 2014-04-03
      • 2018-10-06
      • 2016-12-08
      相关资源
      最近更新 更多