gpt4 book ai didi

ios - 如何实现 Firebase Listener 来观察数据库中的实时变化

转载 作者:行者123 更新时间:2023-11-29 05:10:11 26 4
gpt4 key购买 nike

我在理解如何实现监听器以便我可以观察数据库中的更改并将它们实时反射(reflect)到 TableView 时遇到问题。我想观察 isOnline 和 isHorizo​​ntal 的变化,根据该变化我将更改标签值以及图像值。这是我的代码:

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()
}
}

最佳答案

我找到了解决方案...

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()
}
}

我之前正在观察一个事件,当数据库中发生更改时,观察者(DataSnapShot.value)会通知观察者。

关于ios - 如何实现 Firebase Listener 来观察数据库中的实时变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59766112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com