【发布时间】:2021-10-16 01:17:18
【问题描述】:
我正在尝试实现一个功能,允许用户删除他们与其他用户的聊天。
目前代码工作并且firestore文档被成功删除但是一旦删除发生代码崩溃并且我得到这个错误“致命错误:在展开可选值时意外发现nil”旁边的id = data !["id"] 在代码中。我猜想在删除发生后,firestore 会继续监听文档并在删除后找到一个空集合。有谁知道如何阻止这种情况发生?
public func deleteConversation(conversationId: String, completion: @escaping (Bool) -> Void) {
// Get all conversations for current user
let CurrentUser = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let ConversationRef = db.collection("users").document(CurrentUser!).collection("conversations").document(test!)
print("deleting conversation with \(test!)")
ConversationRef.addSnapshotListener { snapshot, error in
guard let document = snapshot else {
print("Error getting documents: \(String(describing: error))")
return
}
let data = document.data()
if let id = data!["id"] as? String, id == conversationId {
print("conversation found")
ConversationRef.delete()
}
completion(true)
print("deleted conversation")
}
}
【问题讨论】:
标签: swift firebase google-cloud-firestore