删除Firestore快照侦听器不起作用(Swift)

时间:2018-09-03 22:54:57

标签: swift firebase google-cloud-firestore

我有以下代码开始侦听Firestore特定目录中的更改:

func previewImageFromVideo(url:NSURL) -> UIImage? {


    let asset = AVAsset(url: url as URL)
    let imageGenerator = AVAssetImageGenerator(asset:asset)
    imageGenerator.appliesPreferredTrackTransform = true
    imageGenerator.maximumSize = CGSize(width: 250, height: 120)

    var time = asset.duration
    time.value = min(time.value,2)

    do {
        let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
        return UIImage(cgImage: imageRef)
    } catch {
        return nil
    }
}

在viewDidLoad中,我运行函数:updateCart(),但是当数据库中的“活动”字段更改为false时,我试图在应用程序中停止侦听器。我尝试了以下代码,但它不会停止侦听器:

func updateCart(){
   database.collection("Users").document(currentUserUUID!).collection("Transactions").addSnapshotListener { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
        }
        snapshot.documentChanges.forEach { event in
            let Active = event.document.data()["Active"] as? Bool
            let Type = event.document.data()["Instore"] as? Bool
            let Store = event.document.data()["Store"] as? String
            let TDate = event.document.data()["Date"] as? String

            if (Active == true) {
                print("New Transaction at \(Store!) - (Instore location:\(Type!)) on \(TDate!)")
                self.searchCart(Document: event.document.documentID)
                self.DocumentID = event.document.documentID
                self.startTransaction()
            }

            if (event.type == .modified && Active == false){
                print("Transaction at \(Store!) - (Instore location:\(Type!)) on \(TDate!) is no longer active or has been finalized")
                let updatedData = ["Total": "\(self.Total)","Saved": "\(self.Savings)"]
                database.collection("Users").document(currentUserUUID!).collection("Transactions").document(event.document.documentID).setData(updatedData, merge: true)
                self.completeTransaction()
                self.DocumentID = " "

            }
        }
    }
}

0 个答案:

没有答案