Search code examples
firebasekotlingoogle-cloud-platformgoogle-cloud-firestoregcloud

Make Cloud Run service and android App connect to the same Firestore db


I created a Service with Cloud Run. Using the Integrations tab of the Service, I can connect to a Firestore db instance. Unfortunately, I can't choose an existing instance, hence I have to create a new one.

How can I make the Service and the Android App share the same Firestore db? I am not able to choose the db used by the Android App, either, as apparently there is no way in Google Cloud Console, or in Firebase console, or in firebase-firestore library for Android.

To add more details, this is the way suggested by Firestore tutorials to get access to the database. There is no way to choose which one:

private var fireStoreDB = FirebaseFirestore.getInstance()

Solution

  • When you're using the following line of code:

    private var fireStoreDB = FirebaseFirestore.getInstance()
    

    You're creating a new instance of the default Firebase Firestore database.

    To add more details, this is the way suggested by Firestore tutorials to get access to the database. There is no way to choose which one.

    Yes, there is. If you want to create a new instance of another Firebase Firestore database, then you have to pass the ID of the database to the getInstance() method:

    val app = FirebaseApp.getInstance();
    val db = FirebaseFirestore.getInstance(app, "test")
    

    For more info please also check: