androidfirebaseandroid-emulatorfirebase-tools

Local emulated Firestore returns empty DataSnapshot to Android emulator but works with Cloud Firestore


I'm trying to test my Android app, which uses data from Firebase Firestore. However, I have an issue when querying data using my local running Firebase Firestore Emulator and the Android Emulator.

When I query data with the Android Emulator from the Cloud Firestore everything works fine. When I query data from the Emulator Firestore I receive an empty DataSnapshot.

Both Firestore instances (local emulator and cloud Firestore) contain the same data.

When I enter 10.0.2.2.:4001 inside the browser of my Android Emulator I come to the Firebase Emulator Suite and can see that the Firestore emulator status is On. In my opinion this means that a connection to my locally running Firestore emulator exists.

My code to init the Firestore instance:

private val db = Firebase.firestore
db.useEmulator("10.0.2.2", 8080)
db.firestoreSettings = firestoreSettings { isPersistenceEnabled = false }
val docVal = db.collection("district_codes")
            .document("district_codes")
            .get()
            .await()

Logs of my running local Firestore emulator:

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://127.0.0.1:4001/               │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ 127.0.0.1:8080 │ http://127.0.0.1:4001/firestore │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at 127.0.0.1:4401
  Other reserved ports: 4501, 9150

Why am I unable to retrieve data from my locally running Firebase Firestore instance, while data retrieval from the Cloud Firestore instance works correctly?

Update I try to test it with a simple test entry: In the path /Test/doc_test, I added a string field named test_field.

The structure of my test entry on my local Firestore emulator: enter image description here

I retrieve it using the following command:

val docVal = db.collection("Test").document("doc_test").get().await()
val foundValue = docVal.get("test_field") as String?
Log.i(TAG_FIRESTORE, "Found following test value: ${foundValue} - ${docVal}")

And get following Log-entry:

Found following test value: null - DocumentSnapshot{key=Test/doc_test, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=false}, doc=null}


Solution

  • The issue was caused by using the wrong project ID. In the Firestore-debug.log file, the following entry highlighted the problem:

    Multiple projectIds are not recommended in single project mode. Requested project ID <my_project>, but the emulator is configured for <my_project>_testing.
    

    This occurred because I was using a different project ID for the emulator than the one configured for Firebase Firestore in the cloud.

    To resolve the issue, I updated the project ID in the emulator configuration to match the one used in Firebase Firestore.