0

I'm aware of this q/a about bypassing the cache in firebase, but I'd like to do the same in firestore, and do so with listeners...

My code at present does this...

FIRQuery *query = [[db collectionWithPath:@"mycollection"] queryWhereField:@"uid" isEqualTo:user.uid];
[query addSnapshotListener:^(FIRQuerySnapshot *querySnapshot, NSError *error) {
    //...

This works fine, except it is prone to give me old data (or data after it has been deleted at the source). What I'd like to do is, something like...

if (/*some time has passed*/ || /*first time this launch*/) {
    // magic to force the query to bypass anything cached
}
FIRQuery *query = [[db collectionWithPath:@"mycollection"]; 
// and so on...

The best seeming answer to that other question suggests using a transaction...

[db runTransactionWithBlock:^ id(FIRTransaction *transaction, NSError **error) {
    // stuck

but FIRTransaction has only one read-related method, which is to getDocument:error: passing a FIRDocumentReference *.

Also - future editors: I'm writing in objective-c, but I'm happy to read suggestions in swift. If you decide to delete the swift tag, please explain how someone expresses a question in either language. (Please note: the statement "we only use language tags to ask questions about the language itself" is provably false).

user1272965
  • 2,814
  • 8
  • 29
  • 49
  • What's the specific behavior you're observing that you're trying to avoid? – Doug Stevenson Feb 21 '18 at 18:02
  • @DougStevenson if the data changes (or is deleted) while the listener is not running, on the next execution, of addSnapshotListener, I'd like to see the changed (or not see the deleted) data. – user1272965 Feb 21 '18 at 21:43

1 Answers1

0

What you're trying to do is not best served by trying to bypass the local cache, because you shouldn't assume the client ever has anything cached. The SDK manages the cache entirely - it is provided as an optimization so that you don't pay more than you have to for document reads.

If you need an audit trail of things that changed in the database, that trail itself should be recorded in the database, so that the data is correct no matter when it's accessed, regardless of any caches.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441