没有childByAutoId()是否可以获取数据库引用。钥匙?

Is it possible to get database reference without childByAutoId (). Key?

有没有办法从父实例动态获取子数据库,而不必使用 childByAutoId()?

{
  "artists" : {
    "artists_id_1" : {
      "name" : "Atif Aslam",
      "genre" : "Rock"
    },
    "artists_id_2" : {
      "name" : "Arijit Singh",
      "genre" : "Rock"
    }
  }
}

我们通常参考路径,并监听数据库中的项目...示例:

Database.database().reference().child("artists").child("artist_id_1").observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                print("\(String(describing: dictionary["name"] as? String))")
            }

        }, withCancel: nil)

它只适用于第一项。因为,它不会是动态的。通常使用的是下面的例子...

{
  "artists" : {
    "-KeZUDrJv555kteAcssL-" : {
      "name" : "Atif Aslam",
      "genre" : "Rock"
    },
    "-KeZUVXFIQdO7JiyRYk-" : {
      "name" : "Arijit Singh",
      "genre" : "Rock"
    }
  }
}
 Database.database().reference().child("artists").childByAutoId().observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                print("\(String(describing: dictionary["name"] as? String))")
            }

        }, withCancel: nil)

反正不知道说清楚没有,有什么可以先评论再回答。谢谢

我不知道我是否正确理解你的问题或你想做什么,但也许你可以试试

Database.database().reference().child("artists").observe(.value) { (snap:DataSnapshot) in 

// This should print out your childIds
print(snap.key)

// This should print out the values after them
print(snap.value)



}

要显示数据库中的所有艺术家,加载 artists 节点并循环 snapshot.children:

Database.database().reference().child("artists").observe(.value) { (snap:DataSnapshot) in 
  for child in snapshot.children {
    print(child.key)
  }
}

对于这个示例和更多示例,我建议阅读 Firebase 文档,特别是 section on reading and writing lists.