So I have this app which uses Firebase. The problem is that it takes some input (suppose X), reads some data from the FirebaseDatabase (suppose Y), and then compares them. If some elements of Y match with corresponding elements of X, it considers them a duplicate and only updates the value of Y.
If the elements of the two do not match, then it creates a new entry in the database with X.
Now, my code has a button which calls the checkDataNew() method which checks the aforementioned values for similarities. It is responsible for reading from the database and then comparing it with the input values. It then uses the result from that function to update/add the values in the FirebaseDB.
If I keep the activity open and then enter similar values, it works perfectly and only updates the value in the FirebaseDB.
However, if I close the activity and then open it again, then it doesn't enter the addValueEventListener function to read data from the FirebaseDB.
I know because I have debugged it. In the first scenario, the code goes to the lines written inside in the rootRef.addValueEventListener code, but in the second, it doesn't.
Needless to say, this is causing issues of duplicate items in my app. Can anyone explain as to why the addValueEventListener code executes when the activity is open and input is provided multiple times, but fails to execute if I close the activity and reopen it?
The relevant code for that is written below (it's in Kotlin, but even if someone has a solution for Java, I can convert it to Kotlin):
checkInsulinBtn?.setOnClickListener()
{
var isDataValidCheck:Boolean=validateData()
if(isDataValidCheck==true)
{
checkDataNew() //calls Firebase DB read function
var newKey=foundKey
if(newKey=="")
{
var mLogBG:BGLevel= BGLevel(emailID,recentFood,recentEvent,BGLevel,insulinLevel,calendarTime)
var key:String=mFirebaseDatabaseReference.push().toString()
var parsedKeyList=key.split("/-")
var parsedKey=parsedKeyList[1]
parsedKey="-" + parsedKey
mFirebaseDatabaseReference.child(parsedKey).setValue(mLogBG)
var mFirebaseDatabaseReference2=mFirebaseDatabase.getReference("BG Keys")
mFirebaseDatabaseReference2.push().setValue(parsedKey)
Toast.makeText(applicationContext,"Your data has been saved to the cloud, and is viewable in the app calendar",Toast.LENGTH_SHORT).show()
}
else
{
var mLogBG:BGLevel= BGLevel(emailID,recentFood,recentEvent,BGLevel,insulinLevel,calendarTime)
var newKey=foundKey
mFirebaseDatabaseReference.child(foundKey).setValue(mLogBG)
Toast.makeText(applicationContext,"Your previous data has been updated with the new one",Toast.LENGTH_LONG).show()
}
}
}
The code for the FirebaseDB read data and comparison is written below:
fun checkDataNew()
{
var rootRef=FirebaseDatabase.getInstance().getReference("BG Data")
// Read from the database
rootRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for(data:DataSnapshot in dataSnapshot.children)
{
var oldEvent=data.child("recentEvent").getValue().toString()
var oldDate:String=data.child("calendarTime").getValue().toString()
var oldEmailID:String=data.child("emailID").getValue().toString()
if(oldEvent.equals(recentEvent) && oldDate.equals(calendarTime) && oldEmailID.equals(emailID)) {
foundKey = data.key.toString()
isKeyFound = true
return
}
}
}
override fun onCancelled(error: DatabaseError) {
// Failed to read value
}
})
isKeyFound=false
return
}