Skip to content

AngularFirestore.firestore.enablePersistence makes snapshotChanges trigger twice #2808

@willemjanvankranenburg

Description

@willemjanvankranenburg

Version info

Angular: 10.0.3

Firebase: 8.2.10

AngularFire: 6.1.4

@ionic/angular: 5.2.3

Ionic CLI: 6.12.1

Node: 12.18.4

How to reproduce these conditions

Steps to set up and reproduce

  • Create a new Ionic blank project
  • Add firebase and AngularFire packages with above mentioned versions
  • npm i
  • Add firebase config for any environment to the app.module.ts
  • Add following to the AppModule class:
constructor(private afs: AngularFirestore) {
      this.afs.firestore.enablePersistence();
}
  • add the following to the home.page.ts:
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
...
...
 constructor(private afs: AngularFirestore) {
    console.log('in constructor');
    this.afs
      .collection('<COLLECTION_NAME>')
      .doc('<DOCUMENT_ID>')
      .snapshotChanges()
      .pipe(
        map((a) => {
          console.log('in snapshotChanges()');
          const data = a.payload.data() as any;
          data.id = a.payload.id;
          return data;
        })
      )
      .subscribe((object: any) => {
        console.log(object);
      });
  }
  • run ionic serve -l from the terminal
  • After the localhost is started, open the javascript console, see "in snapshotChanges()" logged once.
  • Press F5, refresh the page
  • See "in snapshotChanges()" logged twice.

Sample data and security rules

Debug output

Two times the console.log('in snapshotChanges()');

Expected behavior

When calling .snapshotChanges() it should only resolve once initially and only a second time after the document is changed.

Actual behavior

.snapshotChanges() is called/triggerd twice. Once for the local cache, the second time because you're online. This was not the case in older versions (6.0.3) of angular fire.

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