【发布时间】:2021-03-07 20:24:09
【问题描述】:
我很难在我的应用程序中更新一个控制表格的数组。我希望能够更改 Firebase 数据库中的子节点并在我的用户端对其进行更新,而无需编写代码和发送更新。我需要能够更改他们的标签并让它更新表而不会使索引超出范围。
这是我的类结构,供用户参考:
import UIKit
class User: NSObject {
var Name: String?
var Email: String?
var UID: String?
var Tag: String?
}
在我的控制器中,我有以下代码来使用来自 Firebase 的信息初始化表:
func CreateDataArray() {
Database.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//self.setValuesForKeys(dictionary)
user.Name = dictionary["Name"] as? String
user.Email = dictionary["Email"] as? String
user.UID = dictionary["UID"] as? String
user.Tag = dictionary["Tag"] as? String
if user.Tag == "Macro" {
self.macroUsersArray.append(user)
} else if user.Tag == "Micro" {
self.microUsersArray.append(user)
}
self.users.append(user)
self.userName.append(user.Name!)
self.filteredNames.append(user.Name!)
print(dictionary)
print(self.macroUsersArray)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
然后我有一个函数可以监听 Firebase 的任何更改并更新数组:
func updateDataArray() {
Database.database().reference().child("Users").observe(.childChanged, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//self.setValuesForKeys(dictionary)
user.Name = dictionary["Name"] as? String
user.Email = dictionary["Email"] as? String
user.UID = dictionary["UID"] as? String
user.Tag = dictionary["Tag"] as? String
if user.Tag == "Macro" {
self.microUsersArray.remove(at: 2)
} else if user.Tag == "Micro" {
self.macroUsersArray.remove(at: 2)
}
self.users.append(user)
self.userName.append(user.Name!)
self.filteredNames.append(user.Name!)
print(dictionary)
print(self.macroUsersArray)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
我想让最初拥有标签“宏”的用户切换到“微”,从宏列表中删除并显示在微列表中。我无法让数组追加并继续获取 indexPath is out of range 错误,导致我的应用程序崩溃。我真的需要帮助,过去几周我一直在为此苦苦挣扎。
编辑:我决定使用observe.value,以便在我进行更改时读取它。这是我创建用户信息的代码:
func CreateDataArray() {
Database.database().reference().child("Users").observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//self.setValuesForKeys(dictionary)
user.Name = dictionary["Name"] as? String
user.Email = dictionary["Email"] as? String
user.Tag = dictionary["Tag"] as? String
if user.Tag == "Macro" {
self.macroUsersArray.append(user)
} else if user.Tag == "Micro" {
self.microUsersArray.append(user)
}
self.users.append(user)
self.userName.append(user.Name!)
self.filteredNames.append(user.Name!)
print(dictionary)
print(self.macroUsersArray)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
但是,我可以将所有数据提取到快照中,但是当我尝试追加时它返回 nil。我在任何带有“append(User.Name!)
的代码行上都收到 nil 错误我很想知道如何阻止这个 nil 并让我的观察者继续监听我的数据库中的变化。
编辑 2:
这是我的变量:
var allUsersArray = [User]()
var macroUsersArray = [User]()
var microUsersArray = [User]()
这是我的 ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
updateDataArray()
}
这是我的 updateDataArray 代码:
func updateDataArray() {
Database.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//self.setValuesForKeys(dictionary)
user.Name = dictionary["Name"] as? String
user.Email = dictionary["Email"] as? String
user.UID = dictionary["UID"] as? String
user.Tag = dictionary["Tag"] as? String
self.allUsersArray.append(user)
self.users.append(user)
self.userName.append(user.Name!)
self.filteredNames.append(user.Name!)
self.macroUsersArray = self.allUsersArray.filter { $0.Tag == "Macro" }
self.microUsersArray = self.allUsersArray.filter { $0.Tag == "Micro" }
self.observeChangeInUserProperty()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
这里是 observeChangeInUserProperty() 函数:
func observeChangeInUserProperty() {
let ref = Database.database().reference().child("Users").child("Talent")
ref.observe(.childChanged, with: { snapshot in
let key = snapshot.key
let tag = snapshot.childSnapshot(forPath: "Tag").value as! String // ! = never optional
//get the user from the allUsersArray by its key
if let user = self.allUsersArray.first(where: { $0.Tag == key }) {
if user.Tag != tag { //if the tag changed, handle it
user.Tag = tag //update the allUsersArray
if tag == "Macro" { //if the new tag is Macro remove the user from the Micro array
if let userIndex = self.microUsersArray.firstIndex(where: { $0.Tag == key }) {
self.microUsersArray.remove(at: userIndex)
self.macroUsersArray.append(user) //add user to macro array
}
} else { //new type is micro so remove from macro array
if let userIndex = self.macroUsersArray.firstIndex(where: { $0.Tag == key }) {
self.macroUsersArray.remove(at: userIndex)
self.microUsersArray.append(user)
}
}
//reload the tableviews to reflect the changes
self.microTableView.reloadData()
self.tableView.reloadData()
}
}
})
}
我得到了 Macro 和 Micro 的空数组。我不知道我做错了什么。
编辑 3:
这是我从 Firebase 初始化的新用户结构:
import UIKit
import Firebase
class User: NSObject {
var Name: String?
var Email: String?
var UID: String?
var Tag: String?
init?(from snapshot: DataSnapshot) {
let dictionary = snapshot.value as? [String: Any]
self.Name = dictionary!["Name"] as? String
self.Email = dictionary!["Email"] as? String
self.UID = dictionary!["UID"] as? String
self.Tag = dictionary!["Tag"] as? String
}
}
我现在可以追加数组,但它们没有填充我的表。我还可以在 Firebase 中更改用户的标签,而不会导致应用崩溃,但它不会观察到更改后的标签并将用户移动到另一个数组。所以我需要这两件事的帮助。
【问题讨论】:
标签: arrays swift uitableview firebase-realtime-database updating