首页 > 解决方案 > 子更新崩溃表(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 中更改用户的标签而不会导致应用崩溃,但它不会观察到更改后的标签并将用户移动到另一个数组。所以我需要这两件事的帮助。

标签: arraysswiftuitableviewfirebase-realtime-databaseupdating

解决方案


这个答案需要对目标进行一些设置和重述

目标是拥有两个数组,用作两个 tableViews 的数据源。一个数组包含用户类型:宏,另一个数组包含用户类型:微。

如果用户类型从宏更改为微(反之亦然),则 OP 希望从一个表中删除用户并将其添加到另一个表中。

概念上的答案是:向 Firebase 中的 users 节点添加一个观察者,当用户发生更改时,查看它是否是 type 属性,如果是,则从一个表中删除并将其添加到另一个表中。

有很多解决方案,但让我提出这个冗长的答案,可以在代码和属性观察者中减少。

从三个数组开始,一个容纳所有用户,另一个容纳宏和微用户。假设 UserClass 的 String属性typemacromicro

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

推荐阅读