【发布时间】:2018-03-26 13:10:39
【问题描述】:
我有一个从 FireBase 获取数据,将其保存在本地并将其加载到 UITableView 中的方法。效果很好。
现在,这个观察者 (rootGenerals.observe()) 正在实时运行,并且每次服务器上的数据发生变化时都会更新。
我的问题是我不想在获得新数据后重新加载整个表(就像我现在所做的那样),我只想更新表中特定更改的行或替换行位置(IndexPath ) 如果分数发生了变化。
知道怎么做吗?只需要一些指导,了解在这种情况下的最佳处理方式。
rootGenerals.observe(.value, with: { snapshot in
if !snapshot.exists() { return }
let general = snapshot.value as! NSDictionary
if let users : NSDictionary = general.object(forKey: "users") as? NSDictionary
{
if (self.usersFromDB?.count != 0) {
// Update table rows
self.arrRecords = []
}
else {
// Create table from scratch
self.usersFromDB = users
for user : Any in users.allKeys {
let userDict : NSDictionary = users.object(forKey: user as! String) as! NSDictionary
let record : Record = Record()
record.displayName = userDict.object(forKey: "name") as! String
record.time = userDict.object(forKey: "time") as! Int
record.solvedLogos = userDict.object(forKey: "logos_solved") as! Int
record.deviceId = userDict.object(forKey: "device_id") as! String
record.raw = userDict
self.arrRecords.append(record)
}
self.sortRecords()
}
}
})
【问题讨论】:
-
我认为您可以使用
.childChanged事件类型来检测指定引用的孩子何时更新或更改
标签: ios swift uitableview firebase