Execution of 'For each loop'

Hi, I have the following blocks which call a firebase ‘get tag list’ and put the result in a list variable (Recipes) and then for each item of this list I call a firebase ‘Get Value’ and put each of these values in another list (Origins).
The value that I want to display is an element in a complex data structure of the result value, but this is not a problem.
At the end of the ‘for each loop’ I want to display both lists in two listviews components side by side.
If I try to view the lists immediately after the ‘for each’ block, I see only the list of tags and not the list of values.
This should be because, I think, each ‘Get value’ call to firebase is an asynchronous call that gets immediately executed by the loop, but doesn’t return immediately a result value.
So when the ‘for each loop’ ends, Firebase is still busy to return each value for each call.
Being so, my question is: when and how I can show the two lists with all values returned by Firebase calls in the ’ for each’ loop?

well for same situation i have used a clock and a timer of one sec, when clock timer do, i call next data to get, and in a second it gets data.

1 Like

What if one second is not enough because Firebase could be busy for some reason or another or for a slow internet connection for example?

then maybe a minute is also not enough if user have slow connection. the procedure you are using also working for me in many apps, and when someone having bad luck then anything can happen, lets see if anyone can provide a better solution .

1 Like

I think that there is no better solution. Actually it is not advisable to do dozens or hundreds firebase calls with a ‘for each’ loop. Even if you give only one second time delay between two consecutive calls, it will always be a very unresponsive solution.
What I think is that perhaps we should come to a different database design if we want to show related data in a tabular style with one or few listview components.
In my case, for example, I would like to show a recipe and its country of origin in the same screen, side by side.
When entering data, I could join the recipe’s name and its country of origin and use it as the tag of a firebase database of recipes, while the value of each recipe contains other data about it (how to prepare it, list of ingredients, images, etc…).
This way, with one single firebase call to ‘Get tag list’ I immediately get both information (recipe’s name and its country of origin) in a memory list, where it is much easier to separate the two information with some text function, or leave the two information together and show them in one list.
Waiting for a new database call that returns tags and values at the same time like a Select in SQL, could it be a reasonably solution?

But in the Foreach, for each element the value of a tag is requested. And in GotValue it is executed for each item, and the “value” changes every time it is requested. That’s why you have to add each value to a list variable.

I think those blocks are not right.

image

Note that the data you get depends on the project bucket.

Hi pepocero, what you say is right, and what I do is the same thing.
Note that the value in ‘Got Value’ is a complex data structure that I put in RecipeDetails variable, that has the same complex data structure as value.
From there I extract the list item that I need (the first item in RecipeDetails) to put it in the Origins list variable.

These blocks work perfectly. The discussion here is about when we know that all ‘Get value’ calls to firebase end, in order to show the complete list (Origins) to a listView component?

Then you can put the blocks at the end of the GotValue.
The list will fill up and when it reaches the last value the list will be full.

image

I use something like that and the label fills up until it reaches the last value.
Instead of a label you can use whatever you want

Yes, this works fine when I have few data.
Could be different having hundreds of data and therefore hundreds of calls to firebase within a ‘for each loop’.
Thanks

I did not understand your question too much but i have a solution for what i understand

When you r getting tag list use a varable to save that list in that variable. Now run function to get values and save them into another variable
And now when you are showing values in list view u can also call values form variable where you have saved tag list and show them side by aide without any delay
In case If question is quite different for what i answered :joy: then please explain it a bit more.
And dont forget to say thanks​:wink::joy:

Hi ziach451, if you have a closer look at the first post and the blocks, you can see that I am doing exactly what you are suggesting.
I am only warring about doing many consecutive calls to firebase when using a ‘for each’ block, because when the for ‘each block’ ends, it does not mean that you have immediately all the values from the firebase available to show them in a listview.
Pepocero than suggested to add each value from a ‘get value’ call, immediately in the listView component that will show it as soon as it has been received.
In my first example I was trying to add each value in a list variable and then, at the end of the ‘for each’ block, I was passing this list variable to the listView component to show them.
In that way when the ‘for each block’ ended, firebase was still busy loading the values and giving them to the list variable, but, at the same time, the next block after ‘for each’ was trying to show them in a listView component, even if the list variable was not ready with the result values.
Now I changed the blocks according pepocero’s post, so here are they:
for each loop 2

The problem was with the OriginsList component.

Have a look at the difference between the two blocks.

And not to forget …Thanks