Variables with same name in sub flows

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Variables with same name in sub flows

Post by jassing » 25 Nov 2019 05:33

I ran into a situation that got me confused.
Basically, I had two threads doing some calculations... But
ended up using a couple variables with the same name.

See the attached flow. The "run me" shows two ways to achieve a (to me) confusing result.

I would expect a result of 1 or 12, but I get 3 for either method.

So, what's going on that causes this?
Thanks
Attachments
nonUniqueVarInSubFlows.xml
(5.25 KiB) Downloaded 560 times

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Variables with same name in sub flows

Post by Desmanto » 25 Nov 2019 17:20

This is one hidden feature from the branched out flow. If there are 2 elements added to the same element before; the last added element will always executed later. It is kinda like a hidden index numbering for the element. Sometimes the result is not real time, you have to exit the flow and reenter again to make sure the element is rearranged to the last added.

Just try this, remove the connection Script: A=1; to the Execute Flows: Var4. Then readded again, exit flow and reenter. Try to execute again, you should get 4. Which mean now the Flow Var4 is executed only after Var3.

As why only one thread pass the execution count, the first one is stopped because it return false. The first thread to finish will reach the execution count first, increase the count by 1, then evaluated to false. So it stops there. You can see if it really happen by adding false elements to the execution count, maybe notification on screen "false branch" or just action vibrate. By removing and readding back Execute Flows Var4 (as shown above), you rearrange the order of the execution and now making the Var4 run later instead. Hence the result is 4.

Now the interesting part about the variable return. I also expect 12 actually. Since any returned variable should have affected every local variable with the same name in the same execution (even if they are different thread). I think Automagic execute both flows at the same and already passed the variable A to both flows even though one is later. Then when returning back, only the last one is returned. If you want to solve this kind of problem, you should rely on global variable instead, and not using return variable option in the execute flow. As modifiying glovar will be global to every execution.

The other thing you can try to make it exactly 12, without using glovar, is to add one additional element to one of the branch (just to make the execute flows not execute at the same level thread). Example add sleep 500 ms before Execute Flow: Var4. You should see A now become 12.

I am still confused about the threading and maybe will forget it again tomorrow. But I rarely need this kind of threading, so I won't risk my flow by doing like this. I think we are the one who should arrange the flow properly so they don't conflict each other.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply