Skip to content

[Firestore] whereArrayContains not working in offline scenario. #155

Closed
@IvanBean

Description

@IvanBean

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 3.2.1
  • Firebase Component: Firestore
  • Component version: 17.1.1

[REQUIRED] Step 3: Describe the problem

Add a string value to document's array by arrayUnion when the device is offline, the snapshot listener shows it added successfully but later we query by whereArrayContains shows empty result.

Steps to reproduce:

Let's say we have a collection todos containing a single document todo1, which had only one field taskIds as a string array. The schema: https://i.imgur.com/CmAFWgx.png

Step to reproduce:

  1. Add a snapshot listener to observe data change:
firestore.collection("todos").addSnapshotListener { snapshot, exception -> snapshot?.documents?.forEach { Timber.d("todos ${it.data}") } } 

In the beging the log shows: todos {taskIds=[]}

  1. Turn device to offline, kill and re-launch the app.

  2. Create a task document with auto-generate id, add the id to document "todo1" by arrayUnion:

firestore.collection("todos") .document("todo1") .update("taskIds", FieldValue.arrayUnion(taskId)) 

The listener at step 1 now shows log: todos {taskIds=[mMifGffC5WEMjALjR6xR]}, so the taskId is added successfully.

  1. Later, within a click event we query todos collection with whereArrayContains, either by get():
firestore.collection("todos") .whereArrayContains("taskIds", taskId) .get() .addOnCompleteListener { task -> Timber.d("size ${task.result?.documents?.size}") Timber.d("isFromCache ${task.result?.metadata?.isFromCache}") } 

or addSnapshotListener:

firestore.collection("todos") .whereArrayContains("taskIds", taskId) .addSnapshotListener { snapshot, exception -> Timber.d("size ${snapshot?.documents?.size}") Timber.d("isFromCache ${snapshot?.metadata?.isFromCache}") } 

The result are the same:

size 0 isFromCache true 

But I expect the result size be 1 because document "todo1" should be included.

The same steps work well when device online, both query shows size 1 with addSnapshotListener is from cache and get() isn't.

Any reply were be appreciated, thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    close