0

Hi i am trying to recevie documentID from Firebase Firestore to use for deleting item from recyclerView. I tried to add it to my document first, then i will retrieve it and user could delete whichever item that user wants within docID. Firstly equalized the documentId variable to an empty string then added a snapshotListener in addData function, with for loop i equalized every document.id value to my empty documentID.

Problem is occuring there: documentId variable returns empty value everytime. Pls help.

Sorry for any infancy.

Thanks in advance.


addData Function:

fun addData(view: View) {
  var chosenPair=ozelSharedPreferences.clickiAl().toString()
    val currentU = Firebase.auth.currentUser
    val dbCollection = currentU?.let {
        it.email.toString()
    }
    var documentId= ""
    val docRef = database.collection(dbCollection!!).document("Specified").collection("Pairs")
        .document(chosenPair).collection("Analysis").orderBy("time")
    docRef.addSnapshotListener { value, error ->
        if (value != null && !value.isEmpty) {

            val documents = value.documents

            for (document in documents) {
                documentId=document.id

            }
        }
    }

    val time= com.google.firebase.Timestamp.now()

    val rr=rrText.text.toString()
    var doubleRR=rr.toDoubleOrNull()
    if (doubleRR==null) {
        doubleRR=0.0
    }
    val analyzeDTO = AnalyzeDTO(
        chartImage.text.toString(),
        doubleRR,
        reasonForText.text.toString(),
        resultAddingText.text.toString(),
        conceptText.text.toString(),
        documentId,
        time
    )
    viewModel.save(analyzeDTO)

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

}

Retrieving Data Function:

fun retrieveData() {

    var chosenPair=ozelSharedPreferences.clickiAl().toString()
    val currentU = Firebase.auth.currentUser
    val dbCollection = currentU?.let {
        it.email.toString()
    }


    val docRef = database.collection(dbCollection!!).document("Specified").collection("Pairs")
        .document(chosenPair).collection("Analysis").orderBy("time")

    docRef.addSnapshotListener { value, error ->
        if (value != null && !value.isEmpty) {

            val documents = value.documents

            for (document in documents) {

                var docID= document.get("docID") as String

                val url = document.get("tradingViewUrl") as String?
                val rr = document.get("rrRatio") as Double?
                val reason = document.get("reason") as String?
                val result = document.get("result") as String?
                val concept= document.get("concept") as String?
                val time= document.get("time") as Timestamp

                val downloadedAnalyze = AnalyzeDTO(url, rr, reason, result,concept, docID,time)
                var analyzeList = arrayListOf(downloadedAnalyze)
                list.value=analyzeList
            }
        } else if (error!= null) {
            Toast.makeText(getApplication(),error.localizedMessage,Toast.LENGTH_LONG)
        } else {
             Toast.makeText(getApplication(),"Unknown Error", Toast.LENGTH_SHORT)
        }
    }
}
Sevban Bayır
  • 196
  • 3
  • 13
  • 1
    There is no way you can assign `documentId=document.id` inside the callback and then simply use it outside. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Apr 23 '22 at 09:16
  • 1
    Besides that, if you need to delete records from Firestore, then I think that this article, [How to delete a record from Firestore on a RecylerView left/right swipe?](https://medium.com/firebase-tips-tricks/how-to-delete-a-record-from-firestore-on-a-recylerview-left-right-swipe-d65d993f0baf) or [How to delete multiple records from Firestore using RecyclerView multi-selection?](https://medium.com/firebase-tips-tricks/how-to-delete-multiple-records-from-firestore-using-recyclerview-multi-selection-96108e4c6166) might help. – Alex Mamo Apr 23 '22 at 09:17
  • 1
    Thank you so much. Actually i realized my mistake but can not find any other way to put document id to my Model. İ'll carefully read these, thanks. – Sevban Bayır Apr 23 '22 at 09:27

0 Answers0