Home > database >  Firestore: what's difference between Firebase.firestore and FirebaseFirestore.getInstance()
Firestore: what's difference between Firebase.firestore and FirebaseFirestore.getInstance()

Time:12-31

What's difference between Firebase.firestore and FirebaseFirestore.getInstance().

When I want to update db, which one is appropriate for this purpose?

And when I want to check the extracted result value from Firestore.

if I log it, it only prints object name not result string value which is contained in object.

db.collection("users").whereEqualTo("userId", userId?.toInt())
  .get()
  .addOnSuccessListener {
    documents - >
      for (document in documents) {
        Log.d("결과", documents.toString())
      }
  }

If I print this, it throws result like below:

com.google.firebase.firestore.QuerySnapshot@b38fd47b

I want to read fields and values of this QuerySnapshot, how can I do this?


share code and error:

var db = Firebase.firestore

        val stDocRef = db.collection("users").whereEqualTo("userId", userId?.toInt())
        db.runTransaction { transaction ->
            transaction.update(stDocRef,"name", "new name")
            null
        }.addOnSuccessListener {  Log.d("결과", "Transaction success!") }

enter image description here

enter image description here

CodePudding user response:

You are trying to print the QuerySnapshot Object , Hence it is giving you the Memory Address of the Object. To overcome this, try to print document.data as show in the below example.

According to docs use Firebase.firestore to initialize the db

val db = Firebase.firestore // Access a Cloud Firestore instance from your Activity

Refer the following links for CRUD operation in firebase with kotlin :

  1. https://firebase.google.com/docs/database/android/read-and-write
  2. https://firebase.google.com/docs/firestore/query-data/get-data

CodePudding user response:

What's the difference between Firebase.firestore and FirebaseFirestore.getInstance()?

None. Both are creating the same instance. The first instance is created by using an extension property of the Firebase class, while the second one is creating an instance using the getInstance() method of the FirebaseAuth. The latter one is also a singleton.

If I log it, it only prints the object name, not the result string value which is contained in the object.

That's the expected behavior, since your calling toString() on the documents object:

Log.d("결과", documents.toString())
//                                   
  • Related