问题描述
我在API调用之前启动活动指示器并在之后调用之后停止时遇到问题,但是如果我用.childAdded填充TableView,则无法正确执行.由于.childAdded仅在添加child时才调用,因此我的活动指示器始终处于加载状态.例如:
I faced with a problem when I start activity indicator before API call and stop after, however I'm unable to do it properly if I populate TableView with .childAdded. Since .childAdded calls only when child is added, my activity indicator is always loading. For example:
func parseData() {
self.activityIndicator.startAnimating()
DataService.instance.SOME_DATA.observe(.childAdded, with: { (snapshot) in
if let snapshot = snapshot.value as? Dictionary<String, Any> {
let data = someData(data: snapshot)
self.array.append(data)
}
DispatchQueue.main.async {
self.tableView.reloadData()
self.activityIndicator.stopAnimating()
}
})
}
因此,基本上,如果没有任何值,将不会调用.childAdded,并且活动指示器永远不会停止,因为它仅在重新加载tableView时才停止.可以请一些建议如何正确执行吗?非常感谢.
So basically if there is no any value .childAdded will not be called and activity indicator will never stop since it stops only when tableView is reloaded.Could some please advise how to do it properly? Much appreciated.
一些基于建议答案的更新:
我将提供一个基于聊天应用程序的示例,它会更容易理解.我具有以下数据库结构:
I'll give an example based on the chat app, it would be much easier to understand.I have the following database structure:
user_messages
unique_user_id
unique_user_chat_id
unique_message_1
unique_message_2
unique_message_3
unique_message_4
messages
unique_message_1
text...
unique_message_2
text...
unique_message_3
text...
unique_message_4
text...
我有2个视图控制器,每个都有一个TableView
I have 2 View Controllers and each has a TableView
父视图控制器获取所有unique_user_chat_id,并且当客户按下所选聊天时,第二个View Controller会从消息"对象中填充所有子代.因此,基本上,第二个View Controller不需要调用.observeSingleEvent(of.value)函数来获取unique_user_chat_id,仅将Childs .childAdded添加到"messages"对象中即可.
Parent view controller fetch all unique_user_chat_id and when client press on the chosen chat, second View Controller populate all Childs from 'messages' object. So basically second View Controller doesn't need to call function .observeSingleEvent(of: .value to fetch unique_user_chat_id, only Childs .childAdded in 'messages' object.
推荐答案
当您观察到 childAdded
时,正如您所说的那样,它可能永远不会被调用.
As you observe childAdded
, as you said this might never get called.
将 childAdded
更改为 valueChanged
,并观察 childAdded
.将动画调用添加到 valueChanged
.
change childAdded
to valueChanged
and observe childAdded
. Add the animation call to valueChanged
.
仅在获取初始数据时,才应在观察childAdded时设置活动指示器.
You shouldn't set an activity indicator when observing childAdded, only when fetching initial data.
func fetchData() {
activityIndicator.startAnimating()
DataService.instance.SOME_DATA.observe(.value, with: { (snapshot) in
// Handle current value and stop activity indicator
if let snapshot = snapshot.value as? Dictionary<String, Any> {
let data = someData(data: snapshot)
self.array.append(data)
}
DispatchQueue.main.async {
self.tableView.reloadData()
self.activityIndicator.stopAnimating()
// Observe new childs added
self.observeData()
}
})
}
func observeData() {
DataService.instance.SOME_DATA.observe(.childAdded, with: { (snapshot) in
if let snapshot = snapshot.value as? Dictionary<String, Any> {
let data = someData(data: snapshot)
self.array.append(data)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
这篇关于Firebase .child已添加和活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!