Skip to content

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

@sgallo0692

Description

@sgallo0692

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions