我正在使用Google的Firebase的实时数据库,我正在尝试检查一个小孩是否存在.
我的数据库的结构如下
- - / (root)
- - /users/
- –- /james/
- -- /jake/
- - /rooms/
- -- /room1/
- --- (room 1 properties)
- -- /room2/
- --- (room 2 properties)
我想检查room1是否存在.
我已经尝试过以下内容:
- let roomName:String = "room1"
- roomsDB.child(roomName).observeSingleEventOfType(.Value) {
- (snap:FIRDataSnapshot) in
- let roomExists:Bool = snap.value != nil ? "TAKEN" : "NOT TAKEN"
- }
在访问snap.value时,它返回该房间的属性的JSON,但是如何检查房间(/ rooms / room1 /)是否开始?
评论如果需要澄清
- self.ref = FIRDatabase.database().reference()
- ref.child("rooms").observeSingleEventOfType(.Value,withBlock: { (snapshot) in
- if snapshot.hasChild("room1"){
- print("true rooms exist")
- }else{
- print("false room doesn't exist")
- }
- })