简体   繁体   English

无法将 Firebase 文档 ID 添加到 dataClass

[英]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.它正在将此数据传输到 Firebase。 This data class also includes documentId variable which is a empty string by default.该数据类还包括默认为空字符串的 documentId 变量。 I want to add document Id's that Firebase created automatically.我想添加 Firebase 自动创建的文档 ID。 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: AddAnalyzeActivity、addData 函数:

    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: AddAnalyze ViewModel,保存功能:

    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: PairViewModel、retrieveData 函数:

    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()
          }
    }
}

I want to add document IDs that Firebase created automatically.我想添加 Firebase 自动创建的文档 ID。

To solve this, you only need to annotate the field with @DocumentId .要解决这个问题,您只需要使用@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.还要确保拥有最新版本的 Firestore。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM