Let me start by saiyng that I'm a complete beginner with Android and Firebase too. I'm using a small project to learn booth technologies and I came across with this challenge.
Imagine I have the following Firebase Database structure:
{
"projects": {
"p01": {
"name": "Projecto #1",
"created_at": "2017-01-01 20:00:00",
"tags": {
"t01": true,
"t03": true
}
},
"p02": {
"name": "Projecto #2",
"created_at": "2017-02-01 02:20:33",
"tags": {
"t02": true,
}
}
},
"tags": {
"t01": {
"color": "#000CCC",
"name": "Nature"
},
"t02": {
"color": "#DD00FF",
"name": "Technology"
},
"t03": {
"color": "#438DC2",
"name": "Social"
}
}
}
I want to display a list view with all my projects, and on each row I want to display the title of the project and a list of tags with its background color. That I managed to too, but I think the way I achieved it, its not the best practice.
First I create a value event listener to get all all projects, like this:
fbdRef.child("projects").addValueEventListener(new ValueEventListener() {
And on the onDataChange
I have a for loop that reads and adds all the projects to an ArrayList, but in that loop I do something first asyncronously. Something like this:
for(DataSnapshot tagSnapshot: projectSnapshot.child("tags").getChildren()){
fbdRef.child("tags/" + tagSnapshot.getKey()).addValueEventListener(new ValueEventListener() {
I'm reading all tags, one by one, and updating its item on ListView, this looks ugly and it may be slow. I really am struggling with noSql concept of Firebase and the best way to structure my databases.
What is the best way to get all tags content from each project?