【发布时间】:2016-11-23 04:21:12
【问题描述】:
您好,我在 swift 上使用 firebase 并且遇到 .exist() 问题
我正在尝试进行查询并检查一个值,如果它在那里我什么也不做,如果不是我将它添加到列表中。我只是想避免以这种方式复制数据。代码如下:
InfoCenter.ref.child("users/\(InfoCenter.userId)/following").queryOrderedByValue()
.queryEqualToValue(firstTextField.text)
.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists(){
self.displayAlert("You already follow that person!", Title: "Whoops")
print(snapshot.value!)
} else {
InfoCenter.ref.child("users/\(InfoCenter.userId)/following").childByAutoId().setValue(TheId)
InfoCenter.ref.child("users/\(TheId)/followers").childByAutoId().setValue(InfoCenter.userId)
print(snapshot.value!)
}
})
所以对我来说一切看起来都是正确的,但是当它运行时,snapshot.exist() 总是返回 false,但是当我打印 snapshot.value 时!我得到 null 周围有箭头(我无法输入箭头,因为那时我认为它是一个标签)。所以我很困惑.. null 是如何被认为存在的?有人可以告诉我要改变什么来解决这个问题吗?谢谢!!
编辑: 要清楚以下是用户列表。因此,以下是具有指向其他用户的链接的 autoId。上述查询的全部目的是通过 autoId 并确保此人尚未关注此人。这是我试图解释的数据结构的快照:
【问题讨论】:
标签: swift firebase firebase-realtime-database