Ios 如何实现Firebase侦听器以观察数据库中的实时更改
我无法理解如何实现侦听器,以便观察数据库中的更改并将其实时反映到tableview。我想观察isOnline和isHorizontal的变化,并根据这一变化,我将改变标签值以及图像值。这是我的密码:Ios 如何实现Firebase侦听器以观察数据库中的实时更改,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我无法理解如何实现侦听器,以便观察数据库中的更改并将其实时反映到tableview。我想观察isOnline和isHorizontal的变化,并根据这一变化,我将改变标签值以及图像值。这是我的密码: var userInfoArray : [UserModel] = [UserModel]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup afte
var userInfoArray : [UserModel] = [UserModel]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// readData()
tableView.dataSource = self
tableView.delegate = self
self.tableView.rowHeight = 70
retrieveUserInfo()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// print(emailModified.count)
return userInfoArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
cell.userLbl.text = "User \([userInfoArray[indexPath.row].email].count)"
if userInfoArray[indexPath.row].isHorizontal == true {
cell.messageLbl.text = "I am Horizontal"
} else {
cell.messageLbl.text = ""
}
if userInfoArray[indexPath.row].isOnline == true {
cell.onlineImage.image = UIImage(named: "online")
}else {
cell.onlineImage.image = UIImage(named: "offline")
}
return cell
}
func retrieveUserInfo() {
Database.database().reference().child("new user").observeSingleEvent(of: .value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
print(child.children)
// this will iterate through all children in snapshot at at path specified.
if let userList = child.value as? Dictionary <String,Any> {
let email = userList["email"]
print(email)
let onlineStatus = userList["isOnline"] as! Any
let horizontalStatus = userList["isHorizontal"] as! Any
let list = UserModel()
list.email = email as? String ?? "nil"
print(list.email)
list.isHorizontal = horizontalStatus as! Bool
list.isOnline = onlineStatus as! Bool
self.userInfoArray.append(list)
print(self.userInfoArray)
}
self.tableView.reloadData()
}
}
.childAdded会给你一张一张的记录。因此,在完成处理程序的帮助下获取数据并将数据放入数组中 此文件的Firebase文档: 示例代码:
func getChatMessages() {
_firebase.getChatMessages(uniqueId: chatHeadVM.uniqueId) { (messageModel) in
DispatchQueue.main.async {
guard let msgModel = messageModel as? MessageModel else {
return
}
self.messageList.append(MessageViewModel(messageInfo: msgModel))
//Reload your table
}
}
}
func getChatMessages(uniqueId: String, completionHandlerWithResponseData: @escaping completionHandlerWithResponseData) {
let query = ref.child(Firebase.chatNodeStr).child(uniqueId)
query.observe(.childAdded, with: { snapshot in
if let dict = snapshot.value as? [String: Any] {
let messageModel = MessageModel(withDictionary: dict)
completionHandlerWithResponseData(messageModel)
}
}) { (error) in
print(error.localizedDescription)
completionHandlerWithResponseData(nil)
}
}
希望能有所帮助。我找到了解决方案
func retrieveUserInfo() {
Database.database().reference().child("new user").observe(of:
DataSnapShot.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
print(child.children)
// this will iterate through all children in snapshot at at path specified.
if let userList = child.value as? Dictionary <String,Any> {
let email = userList["email"]
print(email)
let onlineStatus = userList["isOnline"] as! Any
let horizontalStatus = userList["isHorizontal"] as! Any
let list = UserModel()
list.email = email as? String ?? "nil"
print(list.email)
list.isHorizontal = horizontalStatus as! Bool
list.isOnline = onlineStatus as! Bool
self.userInfoArray.append(list)
print(self.userInfoArray)
}
self.tableView.reloadData()
}
}
我之前观察过一个事件,当数据库发生变化时,oberverDataSnapShot.value会通知和观察者