【问题标题】:Delete Firestore document and Tableview Cell at the same time同时删除 Firestore 文档和 Tableview Cell
【发布时间】:2020-10-14 17:46:45
【问题描述】:

我的应用中有一个消息传递功能,因此我有一个包含所有消息的文档集合和一个保存来自不同用户的最新消息的第二个集合。

在我的视图控制器中,我有一个显示所有最近消息的表格视图,当我使用我的函数删除一个单元格时,所有数据都会按照我想要的方式在 Firestore 中删除。

我还将它从我的数组中删除并删除该行。

我的问题是,即使数据已从 Firestore 中删除,单元格也不会同时被删除。

当我再次在模拟器中运行我的应用程序时,单元格已被删除

有谁知道为什么不立即删除单元格

func deleteMessages(currentUid: String, userUid: String) {
    db.collection("messages").document(currentUid).collection(userUid).getDocuments { (snapshot, Error) in
        
        if let er = Error {
            
            print(er)
            
        } else {
            guard let snapshot = snapshot?.documents else {return}
            for snap in snapshot {
                
                let message = Message(dictionary: snap.data())
                let docId = message.docId
                
                db.collection("messages").document(currentUid).collection("recentMessages").document(userUid).delete()
                db.collection("messages").document(currentUid).collection(userUid).document(docId).delete()
                
            }
            
        }
    }
} 


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    
    guard let currentUid = Auth.auth().currentUser?.uid else {return}
    let userUid = chats[indexPath.row].user.userUid
    
    if editingStyle == .delete {
        tableView.beginUpdates()
        self.chats.remove(at: indexPath.row)
      
        deleteMessages(currentUid: currentUid , userUid: userUid)
        
        tableView.deleteRows(at: [indexPath], with: .fade)
        tableView.endUpdates()
    }
    
    
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

【问题讨论】:

  • 表格视图的数据源是什么?删除该行将在视觉上删除该行,但是如果您的数据源在表格视图重新加载时仍然具有消息对象,则该行将返回。

标签: swift google-cloud-firestore tableview


【解决方案1】:

删除后需要在 mainQueue 中调用 tableView.reloadData()

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2021-06-27
    • 2020-08-15
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2020-03-08
    相关资源
    最近更新 更多