arrays - 子更新崩溃表(indexPath 超出范围错误导致我的应用程序崩溃错误)
问题描述
我很难在我的应用程序中更新控制表格的数组。我希望能够更改 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 并让我的观察者继续监听我的数据库中的变化。
编辑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 中更改用户的标签而不会导致应用崩溃,但它不会观察到更改后的标签并将用户移动到另一个数组。所以我需要这两件事的帮助。
解决方案
这个答案需要对目标进行一些设置和重述
目标是拥有两个数组,用作两个 tableViews 的数据源。一个数组包含用户类型:宏,另一个数组包含用户类型:微。
如果用户类型从宏更改为微(反之亦然),则 OP 希望从一个表中删除用户并将其添加到另一个表中。
概念上的答案是:向 Firebase 中的 users 节点添加一个观察者,当用户发生更改时,查看它是否是 type 属性,如果是,则从一个表中删除并将其添加到另一个表中。
有很多解决方案,但让我提出这个冗长的答案,可以在代码和属性观察者中减少。
从三个数组开始,一个容纳所有用户,另一个容纳宏和微用户。假设 UserClass 的 String属性type
为macro
micro
var allUsersArray = [UserClass]()
var macroArray = [UserClass]()
var microArray = [UserClass]()
读入所有用户,然后从中将用户划分为各自的类型
...read firebase single event of .value, with: { snapshot in
and populate allUsersArray from the snapshot..
self.macroArray = self.allUsersArray.filter { $0.type == "macro" }
self.microArray = self.allUsersArray.filter { $0.type == "micro" }
self.observeChangeInUserProperty()
}
下面的代码向 users 数组添加了一个观察者,当用户的任何属性发生更改时,它将通知应用程序,并在快照中显示该用户节点。
func observeChangeInUserProperty() {
let ref = self.ref.child("users")
ref.observe(.childChanged, with: { snapshot in
let key = snapshot.key
let type = snapshot.childSnapshot(forPath: "type").value as! String // ! = never optional
//get the user from the allUsersArray by its key
if let user = self.allUsersArray.first(where: { $0.key == key }) {
if user.type != type { //if the type changed, handle it
user.type = type //update the allUsersArray
if type == "macro" { //if the new type is macro remove the user from the micro array
if let userIndex = self.microArray.firstIndex(where: { $0.key == key }) {
self.microArray.remove(at: userIndex)
self.macroArray.append(user) //add user to macro array
}
} else { //new type is micro so remove from macro array
if let userIndex = self.macroArray.firstIndex(where: { $0.key == key }) {
self.macroArray.remove(at: userIndex)
self.microArray.append(user)
}
}
//reload the tableviews to reflect the changes
self.microTable.reloadData()
self.mactoTable.reloadData()
}
}
})
}
如前所述,这是很多无关代码,可以使用属性观察器进行压缩,或者为两个表或许多其他解决方案利用单个数组。
编辑
如果您想知道如何从 Firebase 填充所有用户数组,您需要有一个从快照初始化的 User 类,这是代码
func loadAllUsersAndPopulateArray() {
let ref = self.ref.child("users")
ref.observeSingleEvent(of: .value, with: { snapshot in
let allUsersSnapshot = snapshot.children.allObjects as! [DataSnapshot]
for userSnap in allUsersSnapshot {
let user = UserClass(withSnap: userSnap)
self.allUsersArray.append(user)
}
})
}
推荐阅读
- javascript - 通过 div 元素传递 ngmodel
- python - 从带有子文件夹的压缩文件夹中提取 .txt 文件,并使用文件夹位置重命名文件
- python-3.x - 无法在 Python 中调试异步函数
- visual-studio-code - Xdebug 远程主机 VSCode
- c# - C# 数据表值以适应并添加页码以进行打印
- android - Kotlin我无法将元素添加到arrayOf的末尾
- c - C 中函数中的 void 与 int
- php - 购物车在子页面上的更新
- amazon-web-services - terraform:无法设置 vpc 名称?
- javascript - 如何在不更改基本输入的情况下更改控制台输出