Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Komag
Level 9

Bug with nested FOR EACH blocks, internal counter gets messed up

I got some strange behavior of endless repeating, and I found out that it was due to two FOR EACH loops running (in different Functions)
The first Function runs a FOR EACH, and one of the operations inside calls another Function which calls another Function which has it's own FOR EACH that is pointing to the same list as the original FOR EACH is.

My solution was to change one of them to FOR i = 0 TO... format

Is this a known behavior that I should have been smart enough to avoid in the first place?

EDIT - I tested using FOR i = 0 TO in both FOR loops, and there is no problem, even with each using the same variable "i", so it's only when using FOR EACH and thus it uses Roku's internal counter system somehow.
0 Kudos
4 Replies
Roku Employee
Roku Employee

Re: Bug with nested FOR EACH blocks, internal counter gets messed up

"Komag" wrote:
...
EDIT - I tested using FOR i = 0 TO in both FOR loops, and there is no problem, even with each using the same variable "i", so it's only when using FOR EACH and thus it uses Roku's internal counter system somehow.


Correct. Each object that implements ifEnum (the enumeration interface) maintains a single internal position pointer. The 'for each' loop just initializes, checks, and advances that position via the object's ifEnum API. If you nest 'for each' loops on the same object, or call the ifEnum interface directly in-between, you will not get the 'expected' behavior.

While I would agree it's not intuitive, and I don't recall any clear documentation or examples off the top of my head, it's "always" worked that way.
And it's been discussed in this forum, I'm sure many years ago though, so I'm not going to go looking for it Smiley Wink

I encourage you to file a documentation enhancement request as well.
0 Kudos
Komag
Level 9

Re: Bug with nested FOR EACH blocks, internal counter gets messed up

Okay, but I'm not sure how to do that.

The page is
https://developer.roku.com/docs/references/brightscript/language/program-statements.md
under
FOR EACH item IN object
"If a second use of FOR EACH is nested within the program statements of the original FOR EACH and points to the same "ifEnum" object, the internal ordinal keeping track of the list position will behave unexpectedly, so it's best to avoid this scenario, perhaps by making use of the FOR counter = exp TO exp format in at least one of them."
0 Kudos
Roku Employee
Roku Employee

Re: Bug with nested FOR EACH blocks, internal counter gets messed up

The only route I know is for you to post on the "SDK documentation: links to fix, missing/broken/etc" topic (https://forums.roku.com/viewtopic.php?f=34&t=69731)

If you want to provide suggested text that's great, but if you just point out what's missing or not clear it should be their job to figure out how to address it.

YMMV. Smiley Wink
0 Kudos
Komag
Level 9

Re: Bug with nested FOR EACH blocks, internal counter gets messed up

Thanks, I did that.
0 Kudos