I'm trying to iterate the firebase-database
and add its elements to my object. It works, but for some reason I fail to understand, it ads each element twice, the log output showing something like this:
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Tahini-Oat Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Tahini-Oat Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Chocolate & Cinnamon Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Chocolate & Cinnamon Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Sweet Potato Muffin }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Sweet Potato Muffin }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Almond Butter Cookies }
TAGZ: DataSnapshot { key = recipeHeaderFirebase, value = Almond Butter Cookies }
The Firebase-database
structure looks like this (note that I have ignored the other elements for now):
My Kotlin code looks like this:
fbdb = FirebaseDatabase.getInstance()
ref = fbdb!!.getReference("cookies")
ref!!.addChildEventListener(object: ChildEventListener {
override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {
val children = snapshot!!.children //DIRECT REFERENCE TO CHILD, USED FOR LOOP
val header = snapshot!!.child("recipeHeaderFirebase")
for(it in children) {
var tempRecipe = RecipeTemplate()
tempRecipe.recipeHeader = header.toString()
Log.d("TAGZ", tempRecipe.recipeHeader) //OUTPUT SHOWS DUPLICATE VALUES
}
}
})
What am I missing? I successfully retrieve the elements in Xcode using similar code...
EDIT FULL CODE ON REQUEST:
package com.healthandchocolate.sjostedtafzelius.healthchocolateandroid
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.firebase.database.*
class RecipeGridView : AppCompatActivity() {
private var fbdb: FirebaseDatabase? = null
private var ref: DatabaseReference? = null
var recipeArray: MutableList<RecipeTemplate> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recipe_grid_view)
Log.d("TAG", "ON CREATE");
fbdb = FirebaseDatabase.getInstance()
ref = fbdb!!.getReference("cookies")
ref!!.addChildEventListener(object: ChildEventListener {
override fun onChildChanged(snapshot: DataSnapshot?, p1: String?) {
}
override fun onChildMoved(p0: DataSnapshot?, p1: String?) {
Log.d("TAG", "ON CHILD MOVED");
}
override fun onChildRemoved(p0: DataSnapshot?) {
Log.d("TAG", "ON CHILD REMOVED");
}
override fun onCancelled(error: DatabaseError) {
print(error)
Log.d("TAG", "ON ERROR");
}
override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {
Log.d("TAGZ", "CALLED") //keyName of each child
val children = snapshot!!.children //DIRECT REFERENCE TO CHILD, USED FOR LOOP
val header = snapshot!!.child("recipeHeaderFirebase")
for(it in children) {
var tempRecipe = RecipeTemplate()
tempRecipe.recipeHeader = header.toString()
//Log.d("TAGZ", tempRecipe.recipeHeader) //keyName of each child
recipeArray.add(tempRecipe)
}
}
}) //END FB CODE
}
}