live-love
live-love

Reputation: 52366

Firestore getDocuments Source.cache and index - offline queries

In my code, I check if I am not entering a duplicate object, like this:

 QuerySnapshot querySnapshot = await myCollection
              .where('x', isEqualTo: x)
              .where('y', isEqualTo: y)
              .where('z', isEqualTo: z)
              .getDocuments(source: Source.cache);
          if (querySnapshot.documents.length == 0) {
           ... add the document to the batch
          }

I find that doing this extra check with thousands of documents slows down the document additions in the server. Without the check, it takes just a few seconds, but with the check, it can take a couple minutes.

I have the index for x,y,z fields enabled in the collection.

Question: Does this index work offline when I do an offline query with Source.cache as well?

Is there a better way to find out if the record is duplicate offline, before adding it to the server?

Upvotes: 0

Views: 268

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Firestore indexes do not work as well offline. The SDK is not equipped to perform massively scalable local queries offline in the same way that the Firestore cloud service can do it while the app is online. This is a known limitation and there is no workaround, and the problem gets worse when there are more documents locally cached for a collection.

You might want to consider queuing up the new documents using some other method, then waiting until the app is online to check them all.

Upvotes: 1

Related Questions