firebase / geofire-objc

GeoFire for Objective-C - Realtime location queries with Firebase
MIT License
436 stars 179 forks source link

Question: Does key exited cover a document deleted from firestore? #142

Open sgallo0692 opened 3 years ago

sgallo0692 commented 3 years ago

Trying to ensure when a document is deleted from firestore, that results in the map annotation from my observe query being removed.

I'm maintaining the results of .documentedEntered in a published array

    @Published var mapStreams = [Stream]()

    init() {
        loadData()
    }

    func loadData() {
        let geoResults = geoFire.query(withCenter: GeoPoint(latitude: 35.111521, longitude: -40.987951), radius: 5000)

        let _ = geoResults.observe(.documentEntered, with: {(key, location) in
            if let key = key, let loc = location {
                let doc = playbackRef.document("\(key)")
                doc.getDocument { (document, error) in

                    let result = Result {
                        try document?.data(as: Stream.self)
                    }

                    switch result {
                        case .success(let stream):
                            if let stream = stream {
                                // A `City` value was successfully initialized from the DocumentSnapshot.
                                self.mapStreams.append(stream)
                            } else {
                                // A nil value was successfully initialized from the DocumentSnapshot,
                                // or the DocumentSnapshot was nil.
                                print("Document does not exist")
                            }
                        case .failure(let error):
                            // A `City` value could not be initialized from the DocumentSnapshot.
                            print("Error decoding stream: \(error)")
                    }
                }
            }
        })
}

What I realized is this only is accurate when my app is opened, but it's not acting as a snapshotListener if a document is modified or deleted. How do I ensure I monitor firestore for deletes or updates to a document?