0

I have managed to read data from my firebase database but cant seem to re-use the String which has been read.

My successful read is as per below. When i check the logcat for the Log.d("Brand") it actually shows the String as expected.

brandchosenRef=FirebaseDatabase.getInstance().reference
val brandsRef = brandchosenRef.child("CarList2").orderByChild("Car").equalTo(searchable_spinner_brand.selectedItem.toString())

    val valueEventListener = object : ValueEventListener {


    override fun onDataChange(dataSnapshot: DataSnapshot) {
        for(ds in dataSnapshot.children){
            Log.d("spinner brand",searchable_spinner_brand.selectedItem.toString())
            val Brand = ds.child("Brand").getValue(String::class.java)
            val brandselected= Brand.toString()

            Log.d("Brand","$brandselected")
         selectedbrand== brandselected

            Log.d("selected brand",selectedbrand)
        }
    }
    override fun onCancelled(databaseError: DatabaseError) {
        Log.d("Branderror","error on brand")
    }

}
brandsRef.addListenerForSingleValueEvent(valueEventListener)

What i am trying to do is write "selectedbrand" into a separate node using the following:

val carselected = searchable_spinner_brand.selectedItem.toString()

val dealref = FirebaseDatabase.getInstance().getReference("Deal_Summary2")

val dealsummayId = dealref.push().key

val summaryArray = DealSummaryArray(dealsummayId.toString(),"manual input for testing","brand","Deal_ID",carselected,extrastext.text.toString(),otherinfo.text.toString(),Gauteng,WC,KZN,"Open")

dealref.child(dealsummayId.toString()).setValue(summaryArray).addOnCompleteListener{
    }

Note, in the above i was inputting "manual input for testing" to check that my write to Firebase was working and it works as expected. if i replace that with selectedbrand, then i get the below error.

kotlin.UninitializedPropertyAccessException: lateinit property selectedbrand has not been initialized

the summary array indicated above is defined in a separate class as follows. and as seen "manual input for testing is declared as String.

class DealSummaryArray(val id:String,val brand:String,val Buyer_ID:String,val Deal_ID:String,val Car:String,val extras:String,val other_info:String,val Gauteng:String,val Western_Cape:String,val KZN:String,val Status:String) {
constructor():this("","","","","","","","","","",""){
}

}

My question simply put, it why can i not re-use the value i read from the database? even if i was not trying to re-write it to a new node i cannot seem to utilize the value outside of the firebase query.

I seem to get this problem everywhere in my activities and have to find strange work around's like write to a textview and then reference the textview. please assist.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Waseem Ahmed
  • 379
  • 1
  • 5
  • 17

1 Answers1

0

Data is loaded from Firebase asynchronously, as it may take some time before you get a response from the server. To prevent blocking the application (which would be a bad experience for your users), your main code continues to run while the data is being loaded. And then when the data is available, Firebase calls your onDataChange method.

What this means in practice is that any code that needs the data from the database, needs to be inside the onDataChange method or be called from there. So any code that requires selectedbrand needs to be inside onDataChange or called from there (typically through a callback interface).

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank. i am going to mark this as answered, becasue what you described is what i ahve been doing. i was hoping that there was an easier way. having said, that, taking into consideration that you mentioned the code must reside within onDataChange, why can't i use the Intent function within data change? – Waseem Ahmed Nov 25 '19 at 12:01