【发布时间】:2017-09-18 08:59:32
【问题描述】:
现在,我对使用 childAdded 数据事件类型观察的 firebase 感到非常困惑。我之所以使用 childAdded 来观察我的 firebase,是因为我想让我的列表页面 动态 firebase 是否有新数据插入。
我的问题是如何知道当达到 queryLimit 时观察是否停止调用?因为我有一个指示器,我想在达到 queryLimit 时将其关闭。
我的firebase结构如下:
root {
talkID1(id by auto create) {
attribute1 ...
attribute2 ...
attribute3 ...
time
}
talkID2(id by auto create){
...
time
}
... // many talk ID which auto create by firebase
}
据我所知,如果使用 childAdd 进行观察,数据将一个孩子一个孩子地在回调中传递数据。因此,如果我在 firebase 中有 N 个数据,我认为它会调用 N,对吗?
下面是我的完成处理程序:
func receiveIfExist(completion: @escaping (_ data: (My data type) -> Void) {
let requestWithQuery = Database.database.reference().child("root").queryOrdered(byChild: "time")
requestWithQuery.queryLimited(toLast: 5).observe(.childAdded, with: { (snapshot) in
guard let value = snapshot.value as? [String: Any] else { return }
self.getSingleTalkInfo(key: snapshot.key, value: value, completion: { (data) in
completion(data)
})
})
}
我在 viewDidLoad() 中调用 receiveIfExist 这个函数。
override func viewDidLoad() {
super.viewDidLoad()
self.myIndicator.startAnimating() // start running indicator
self.receiveIfExist { (data) in
self.handleTalk(with: data) // do something with request data
self.myIndicator.stopAnimating() // WEIRD!!!! Please look comments below
/*
I think it can not be added here because this completion will call N<=5 times just what I said before.
I think it should detect what my queryLimit data is and check the request data is this queryLimit data or not.
If yes then stop indicator animating, if not then keep waiting the queryLimit reach.
*/
}
}
如何检测观察是否达到 queryLimit?
如果我能检测到,那么我可以在指示灯到达时关闭它。
谢谢!
【问题讨论】:
标签: swift firebase callback completionhandler indicator