简体   繁体   中英

Firestore - DocumentSnapshot.exists return true after deleting the document

Context

I have a method of listening to the changes in a collection. It works correctly for an updated document. I am now trying to implement the deleting of the item on my UI when the document is deleted on Firestore. To do so I noticed the "exists" property of the DocumentSnapshot class which seems to be the information that I look for.

Expected result

I delete a document. Then I get notified and the "exists" property of the snapshot is false. So I can remove the item on my UI.

Actual result

I delete a document. Then I get notified but the "exists" property of the snapshot is true.


Am I misunderstanding the "exists" property or is it a bug?

EDIT: Sample of my code

Service:

static Future<void> getPoles(LatLng center, double zoom, Function(Stream<Pole>) callback) async {
// ...
    for (var direction in directions) {
      print("DIRECTION: $direction : ${_getEndcode(direction)}");

      var snapshots = Firestore.instance.collection(POLES_COLLECTION)
        .where(COL_POSITION_HASH, isGreaterThanOrEqualTo: direction, isLessThan: _getEndcode(direction))
        .snapshots();
      snapshots.listen((snapshot) {
        var poles = _onPoleChanges(snapshot);
        for (Pole pole in poles) {
          polesController.add(pole);
        }
      });
    }
    callback(polesController.stream);
});
}
static List<Pole> _onPoleChanges(snapshot) {
    List<Pole> poles = [];
    print("Length: ${snapshot.documentChanges.length}");
    for(var change in snapshot.documentChanges) {
      DocumentSnapshot doc = change.document;
      GeoPoint point = doc.data[PoleService.COL_POSITION]['geopoint'];
      var states = doc.data[PoleService.COL_STATE];
      if (states == null) states = [];
      Pole pole = Pole(
        exists: doc.exists,
        id: doc.documentID,
        note: doc.data[PoleService.COL_NOTE],
        street: doc.data[PoleService.COL_STREET],
        npa: doc.data[PoleService.COL_NPA],
        locality: doc.data[PoleService.COL_LOCALITY],
        geopoint: LatLng(point.latitude, point.longitude),
        geohash: doc.data[PoleService.COL_POSITION]['geohash'],
        withCharges: doc.data[PoleService.COL_WITHCHARGES],
        states: List.from(states),
        disabled: doc.data[PoleService.COL_DISABLED]
      );
      poles.add(pole);
    }
    return poles;
  }

The object "pole" is given to the callback and give it to the UI. The UI listen on the Stream and call the following function when a change is detected:

void _onPoleChanges(Pole pole) {
    if (!pole.exists) {
      _markers.remove(MarkerId(pole.id));
      return;
    }
  // ...
}

The solution is to use the documentSnapshot.metadata.isFromCache property if the persistance is enabled. Thanks to Fardeen Khan who helped to figure out in the comments.

EDIT: Well I had a case where it worked but not always. That is weird and so my problem is not completely resolved.

EDIT 2: I have to use the type property of DocumentChange class. It works perfectly now: Source: https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM