skip to Main Content

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

When do 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 the object name not result in string value that is contained in the 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

2

Answers


  1. 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
    Login or Signup to reply.
  2. 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())
    //                        👆
    

    The documents that comes from the lambda expression is actually a document of type QuerySnapshot which:

    contains the results of a query. It can contain zero or more DocumentSnapshot objects.

    So you have to loop through the documents, as you already and get the data according to the type of the field:

    .addOnSuccessListener { documents - >
        for (document in documents) {
          Log.d("결과", document.getString("userId"))
          //                        👆
        }
    }
    

    If you however want to run a transaction, which in my opinion doesn’t make any sense, since you’re only reading data, then please check the documentation, or my answer from the following post:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search