Sevban Bayır
Sevban Bayır

Reputation: 686

Can't add Firebase document Id to dataClass

I have a data class for data that come from user entries. İt is carrying this data to Firebase. This data class also includes documentId variable which is a empty string by default. I want to add document Id's that Firebase created automatically. I tried every way I could think of. But it takes default value in any way.

Here are the four code snippets about this issue. Data class, adding data activity, and retrieving data activity and their View Models.

Dataclass:

data class AnalyzeModel(
var concept: String?="",
var reason: String?="",
var result: String?="",
var rrRatio: Double?=0.0,
var tarih: Timestamp=Timestamp.now(),
var tradingViewUrl: String="",
var id : String="")

AddAnalyzeActivity, addData function:

    fun addData(view: View) {

    val tarih = com.google.firebase.Timestamp.now()
    val rr = rrText.text.toString()
    var doubleRR = rr.toDoubleOrNull()
    if (doubleRR == null) { doubleRR = 0.0 }

    val analyzeDTO = AnalyzeModel(
        conceptText.text.toString(),
        reasonForText.text.toString(),
        resultAddingText.text.toString(),
        doubleRR,
        tarih,
        chartImage.text.toString()
    )
    viewModel.save(analyzeDTO)

    val intent = Intent(this, PairDetailActivity::class.java)
    startActivity(intent)
    finish()
}

AddAnalyze ViewModel, save function:

    fun save(data: AnalyzeModel) {

    database.collection(dbCollection!!).document("Specified").collection("Pairs")
        .document(chosenPair!!)
        .collection("Analysis")
        .add(data)
        .addOnFailureListener { exception ->
            exception.printStackTrace()
            Toast.makeText(getApplication(), exception.localizedMessage, Toast.LENGTH_LONG).show()
        }
}

PairViewModel, retrieveData function:

    private fun retrieveData() {

    val docRef = collectionRef.orderBy("tarih", Query.Direction.DESCENDING)
    docRef.addSnapshotListener { value, error ->
        try {
            if (value != null && !value.isEmpty) {
                val allAnalysis= ArrayList<AnalyzeModel>()
                val documents = value.documents
                documents.forEach {
                    val analyze = it.toObject(AnalyzeModel::class.java)
                    if (analyze!=null){

                        allAnalysis.add(analyze)
                    }
                }

                list.value = allAnalysis
            } else if (error != null) {
                Toast.makeText(Application(), error.localizedMessage, Toast.LENGTH_LONG).show()
            }
        } catch (e: Exception) {
            e.printStackTrace()
          }
    }
}

Upvotes: 0

Views: 335

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138834

I want to add document IDs that Firebase created automatically.

To solve this, you only need to annotate the field with @DocumentId.

data class AnalyzeModel(
    var concept: String?="",
    var reason: String?="",
    var result: String?="",
    var rrRatio: Double?=0.0,
    var tarih: Timestamp=Timestamp.now(),
    var tradingViewUrl: String="",
    @DocumentId 👈
    var id : String=""
)

Be also sure to have the latest version of Firestore.

Upvotes: 3

Related Questions