Skip to content

snapshotChanges().take(1).toPromise() returns partial and inconsistent results #1552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mydoal opened this issue Apr 9, 2018 · 9 comments
Closed

Comments

@mydoal
Copy link

mydoal commented Apr 9, 2018

Version info

Angular:
"@angular/common": "5.0.3",

Firebase:
"firebase": "^4.11.0",

AngularFire:
"angularfire2": "^5.0.0-rc.6",

Other (e.g. Ionic/Cordova, Node, browser, operating system):
"ionic-angular": "3.9.2",

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

Steps to set up and reproduce

When using angularfire api, the results of the query on the same data can be sometimes: none, one or all:

angularFire.collection(X).where(Y).snapshotChanges().take(1).toPromise().then(...) --> returns inconsistent results

When using firebase api on the same query with the same data, all the results are returned consistently.

firebase.firestore().collection(X).where(Y).get().then(...) --> always returns all results

Expected behavior

It is expected that the .snapshotChanges().take(1).toPromise() on the angularfire query api will give the same results as get().then() on firestore query api.

Actual behavior

The results of the query over the same data is inconsistent. Sometimes it returns no results / one results / all results.

@davideast
Copy link
Collaborator

This is due to the mechanics explained in #1405. We plan on changing to a cold observable emission soon. Read and follow that issue for updates.

@kylecordes
Copy link

I've experienced an interesting variation of this. I have a collection query with snapshotChanges() which always emits twice. First it emits just a single result (one document), then a moment later emits its actual correct result. I'm still investigating at what layer this happens etc.; but it seems worth a mention as it may be related to this earlier issue.

@jamesdaniels
Copy link
Member

@kylecordes the first emission is your cached results, the second is presumably the update from the server.

@kylecordes
Copy link

@jamesdaniels @davideast It would be much preferable to have programmatic control (or at least visibility) on this. As we try to create visual polish by running animations as the data changes, this double delivery of the data causes exaggerated visual disruption.

Yet there seems to be no straightforward path to postprocess the observable and get good results; skip(1) doesn't work, because on an initial load there will only be a single emit. debounceTime() works, but requires guessing a time delay, a guess likely to be wrong on varying hardware and networks.

Of course, I could be missing some obvious workaround, perhaps with a bit more Rx manipulation I could discard the initial delivery only if it happens within some very short time bound (indicating it came from cash) or similar. Still, even such a workaround would be much worse than some explicit way to handle this.

@jamesdaniels
Copy link
Member

jamesdaniels commented Aug 25, 2018 via email

@kylecordes
Copy link

@jamesdaniels Doe get() make it possible to retrieve the first "real" result, and then further updates on there, without ever getting the erroneous partial result at the beginning?

@jamesdaniels
Copy link
Member

jamesdaniels commented Sep 3, 2018 via email

@jimmykane
Copy link

@jamesdaniels will the issue get updated once this parameter is implemented?

@MvRemmerden
Copy link

I'm having a problem with get(), as when I'm using it in combination with Angular Universal to prerender pages, it takes around one minute per page to complete. I suspect there might be a problem with a promise that doesn't resolve, both valueChanges() and snapshotChanges() work perfectly, but only give the cached results, as my prerendering script only waits for the first emission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants