Your Digital Media Has Never Looked So Good

 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

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

Fri Aug 16, 2019 12:51 am

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.
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

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

Wed Sep 04, 2019 5:30 pm

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 ;-)

I encourage you to file a documentation enhancement request as well.
 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

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

Fri Sep 06, 2019 3:06 am

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."
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

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

Mon Sep 09, 2019 4:43 pm

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. :wink:
 
User avatar
Komag
Topic Author
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

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

Tue Sep 10, 2019 3:56 am

Thanks, I did that.

Who is online

Users browsing this forum: No registered users and 5 guests