diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java index 0e92e76c2c2..4b3e37ba9f8 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java @@ -28,6 +28,7 @@ import com.google.firebase.firestore.model.Document; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.MaybeDocument; +import com.google.firebase.firestore.model.NoDocument; import com.google.firebase.firestore.model.SnapshotVersion; import com.google.firebase.firestore.model.mutation.Mutation; import com.google.firebase.firestore.model.mutation.MutationBatch; @@ -381,17 +382,19 @@ public ImmutableSortedMap applyRemoteEvent(RemoteEve MaybeDocument doc = entry.getValue(); MaybeDocument existingDoc = existingDocs.get(key); - // If a document update isn't authoritative, make sure we don't - // apply an old document version to the remote cache. We make an - // exception for SnapshotVersion.MIN which can happen for - // manufactured events (e.g. in the case of a limbo document - // resolution failing). if (existingDoc == null - || doc.getVersion().equals(SnapshotVersion.NONE) || (authoritativeUpdates.contains(doc.getKey()) && !existingDoc.hasPendingWrites()) || doc.getVersion().compareTo(existingDoc.getVersion()) >= 0) { + // If a document update isn't authoritative, make sure we don't apply an old document + // version to the remote cache. remoteDocuments.add(doc); changedDocs.put(key, doc); + } else if (doc instanceof NoDocument && doc.getVersion().equals(SnapshotVersion.NONE)) { + // NoDocuments with SnapshotVersion.MIN are used in manufactured events (e.g. in the + // case of a limbo document resolution failing). We remove these documents from cache + // since we lost access. + remoteDocuments.remove(doc.getKey()); + changedDocs.put(key, doc); } else { Logger.debug( "LocalStore",