观察者未在快速的iOS Firebase实时数据库中调用 [英] Observer not calling in swift iOS firebase realtime database
问题描述
我正在构建一个使用Firebase实时数据库的应用程序.这是我的数据库内容.
I'm building an app which uses firebase realtime database. Here is my database content.
在这里,每当将一个新的子节点添加到请求"节点时,我都需要获取Phno和Message值并在控制台中将其打印出来.这是我的观察者代码
Here when ever a new child node is added to Request node I need to fetch the Phno and Message value and print it in the console. Here is my observer code
user.observe(.childAdded) { (DataSnapshot) in
print("Inside")
if let dict = DataSnapshot.value as? NSDictionary
{
for i in dict.allKeys
{
if let data = dict[i] as? NSDictionary
{
let message = data["Message"] as! String
let phno = data["Phno"] as! String
if(message != "" && phno != "")
{
print(message)
print(phno)
}
}
}
}
我将上面的代码放在函数内,并在viewdidload()中首次调用它.之后,我尝试手动将子节点添加到请求节点,但观察者未触发.打印语句进行调试,我在日志输出中发现了一些奇怪的情况
I placed above code inside a function and called it in viewdidload() for first time. After which I tried manually adding child node to request node but the observer is not triggering.And I used that "inside" print statement for debugging and I found something strange in log output
您可以看到,在视图加载期间一次调用了Inside,但是再次调用了Inside,甚至没有对数据进行任何更改.如何解决此问题,以便在将新的子节点添加到请求节点时调用触发器
As you can see the Inside is called once during view did load but again it is called without even making any changes to the data. How can I solve this problem so that trigger get called when ever a new child node is added to request node
推荐答案
我认为问题在于,如何获取请求父节点中包含的子节点数据.如果是这样,请尝试以下操作:
I think the question is asking how to get to the child node data contained within the Request parent node. If so, try this:
首先,我们想在某个地方存储数据,所以我创建了一个类来保存每个请求,然后创建一个数组来保存所有请求
First, we want somewhere to store that data so I've created a class to hold each request and then an array to hold all of the requests
class RequestClass {
var key = ""
var msg = ""
var phone = ""
convenience init(withSnapshot: DataSnapshot) {
self.init()
self.key = withSnapshot.key
self.msg = withSnapshot.childSnapshot(forPath: "Message").value as? String ?? "no msg"
self.phone = withSnapshot.childSnapshot(forPath: "Phno").value as? String ?? "no phone"
}
}
var requestArray = [RequestClass]()
然后,将观察者添加到请求"节点..childAdded观察者将首先迭代每个子项,然后呈现任何新添加的子项,此后又将其添加到数组中. self.ref
是一个指向我的Firebase根的类var.
Then we add an observer to the Request node. A .childAdded observer will initially iterate over each child and then present any newly added children thereafter which in turn will add it to the array. self.ref
is a class var that points to my firebase root.
func readRequests() {
let requestRef = self.ref.child("bill").child("Request")
requestRef.observe(.childAdded, with: { snapshot in
let request = RequestClass(withSnapshot: snapshot)
self.requestArray.append(request)
})
}
以上内容将读取问题中的节点并将每个请求存储在数组中.然后,要进行测试,我们可以将其打印出来
The above will read the node in the question and store each request in the array. Afterwards, to test, we can print those out
self.requestArray.forEach { print($0.key, $0.msg, $0.phone)}
输出为
EEC8A no message no phone
REQ2 Hello 2353476346346
REQ3 K 12312
这篇关于观察者未在快速的iOS Firebase实时数据库中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!