[Swift] Get key as well as value in Realtime database

db

{
  "people": {
    "-MPRm-8iCWsKK6B_nuHA": {
      "age": 30,
      "mail": "[email protected]",
      "name": "taro"
    },
  }
}

swift

let ref = Database.database().reference().child("people")

ref.observe(DataEventType.value) { (snapshot) in
    for child in snapshot.children {
        let snap = child as! DataSnapshot
        let key = snap.key    
        let value = snap.value

        // key : -MPRm-8iCWsKK6B_nuHA
        // value : Optional({ age = 30; mail = "[email protected]"; name = taro; })
    }
}

Recommended Posts

[Swift] Get key as well as value in Realtime database
[Swift] Get document Id as well as document in Cloud Firestore
Handle C char ** well in Swift
Sample code to get key JDBC type values in Java + H2 Database
How to get the setting value (property value) from the database in Spring Framework
Use MyBatis to get Map with key as identifier and value as Entity
Get contextual information such as HttpServletRequest in Jersey
Handle pointers such as UnsafePointer <T> in Swift
I want to get the value in Ruby
[Java] How to get the key and value stored in Map by iterative processing