【问题标题】:Reload tableView based on the changes of Firestore documents added or deleted根据添加或删除的 Firestore 文档的变化重新加载 tableView
【发布时间】:2021-02-08 16:27:19
【问题描述】:

如何在新文档添加到 Firestore 或删除时重新加载表格视图这是我的问题:

我在viewDidLoad() 上调用此函数feachOrder(),因此在添加或删除文档后,它不会在firestore 中监听该更改,直到我关闭应用程序并再次重新打开它或再次运行它,我想要什么是一旦添加了新文档,tableView 会重新加载数据并立即获取值

func fetchOrder(){
        
        let fireStore = Firestore.firestore()
        let doc = fireStore.collection("طلب").document(Auth.auth().currentUser!.uid)
        self.doc = doc.documentID
        
        doc.getDocument { (snapshot, error) in
            if error != nil {
                debugPrint("error fitching\(String(describing: error))")
            }else{
                guard let snap = snapshot?.data() else {return}
                
                let name = snap["name"] as? String ?? ""
                let phone = snap["phone"] as? String ?? ""
                let orderText = snap["order"] as? String ?? ""
                let time = snap["time"] as? String ?? ""
                let price = snap["amount"] as? String ?? ""
                guard let orderLocation = snap["orderLocation"] as? GeoPoint else{return}
                guard let userLocation = snap["userLocation"] as? GeoPoint else {return}
                
                DispatchQueue.main.async {
                    UIView.animate(withDuration: 2 , delay: 2, options: .allowAnimatedContent, animations: {
                                   self.emptyView.isHidden = true
                               }, completion: nil)
                               
                    let myOrder = myOrderData(name: name, phone: phone, amount: price, time: time, orderDetails: orderText, orderLocation: orderLocation, myLocation: userLocation)
                                   self.orders.append(myOrder)
                    self.ordersTV.reloadData()
                    
                }
                               
                
            }
           
            
        }
        
        
    }

【问题讨论】:

  • 您需要在代码中添加监听器。
  • 究竟在哪里以及这就是我问这个问题的原因我看到很多类似的问题,但我猜它们都与我的代码不匹配

标签: ios swift google-cloud-firestore


【解决方案1】:

你想要的是一个快照监听器。这个文档应该对你有帮助https://firebase.google.com/docs/firestore/query-data/listen#swift_1

在您的示例中,您只需通过let collection = fireStore.collection("طلب") 访问该集合。然后您可以通过执行以下操作来创建快照侦听器:

collection.addSnapshotListener { (snapshot, error)
  // TODO Handle error

  // Handle each change based on what kind of update it is
  snapshot?.documentChanges.forEach({ (change) in
     switch change.type {
       case .added:
         // A document has been added to the collection
         self.tableview.reloadData()
       case .modified:
         // A document in the collection has been modified
       case .removed:
         // A document has been removed from the collection
     }
  }
}

您只需调用此函数一次,其余的由 Firebase 处理。

您可以根据需要从您的 ViewDidLoad 或其他文件中调用它。如果您从另一个文件中调用它,与您的视图控制器交流更新的最佳方式是使用本地通知。

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2015-10-10
    • 2020-03-08
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多