如何知道firebase数据库中是否存在路径

时间:2018-06-12 19:11:41

标签: swift firebase firebase-realtime-database

我的数据结构如下:

-posts
         -postID
              -name
               -text

我正在通过

检索帖子
Database.database().refrence.child(“posts”).observe(.childAdded){ (snap) in
  If snap.value is nsnull{}else{
    The retriving data code here
  }
}

点击时。在帖子上

Database.database().refrence.child(“posts”).child(postID).observe(.childAdded){ (snap) in
  If snap.value is nsnull{}else{
    The retriving data code here
  }
}

一切都很顺利,除了如何知道具有某个postid的某个帖子是否会被删除,因为它不会在observe函数中的代码中通过。 我想显示一条消息,通过查看路径是否存在来删除帖子。

注意:如果删除帖子,我不想更改密钥的值。

1 个答案:

答案 0 :(得分:1)

如果您想知道何时删除任何帖子,请收听与您当前.childRemoved类似的.childAdded事件。

如果您想知道特定帖子(一旦您知道其ID)是否存在,您可以通过观察其.value事件来做到这一点。 E.g。

ref.child("rooms/room1").observeSingleEvent(of: .value, with: { (snapshot) in
    if snapshot.exists(){
        print("true rooms exist")
    }else{
        print("false room doesn't exist")
    }
})