Problem with duplicate flow variables

Hey folks,

today I wanted to iterate through some node settings (KNIP) using two flow variables. It did not work as expected and I figured out, that the Replace option of the Java Snippet node creates duplicate variables instead of replacing them.

Please see the attached images and the simplified workflow to understand what I mean. This time, quick help is really appreciated!


Flow Variables are never really replaced. They are put on a stack and you only have access to the top-most value of a flow variable. This means that from the node's perspective pushing a second variable with the same name is almost identical to replacing it. What exactly is your problem?

As you can see in the first image (duplicatevariables01.png) the Variable Condition Loop End node does not use the top-most value of x.

Hence, the loop will never stop, because x will never equal any condition specified in the configuration dialog.

The Loop End node *does* use the top-most value. As I said, the value of flow variables cannot be changed, you can only push a new value onto the stack. The new value is then only visible to all downstream nodes, but will never affect any predecessing nodes. This also means that in your example the value of "x" is the same in every loop iteration. Even if you "change" the value in the Java Snippet, the new value will not be visible in the next iteration because the flow variable stack is re-created in the loop start node (otherwise the stack would very soon explode).

I faced Problem with duplicate flow variables but I found help from some engineers. But you have also told me the right thing. broadcasting

Hi all,

today I was facing almost the exact same situation as @ImNotGoodSry did 6 years ago.
Since it took me a little while to figure out how to solve this, I wanted to share the basic idea here.

The question was: How to continue the next loop iteration right where the last iteration ended? (e.g. when you read some data in chunks, not knowing exactly how big each chunk is. – In my case this is because I retrieve data from a webservice that handles internally the amount of data that is returned…)

As @thor wrote, flow variables will not be passed back to the beginning of the loop. Changes to flow variables only affect downstream nodes (also in loops). The solution is to use a “Recursive Loop”. The special combination of Recursive Loop Start and End nodes provides a way to have the collected data from any preceding iteration available in the loop. That way, you can determine where to continue the next iteration.

Hope this helps someone.

1 Like