sub init() m.top.functionName = "executeTask" m.counter = 0 print("In cacher image task") end sub
function executeTask() url = m.top.url print("Filename: "+ m.top.fileName) urlTransfer = CreateObject("roURLTransfer") urlTransfer.SetURL(url) urlTransfer.getToFile(m.top.fileName) m.top.response = url m.top.request = url end function
The main problem is that I can not change fields of the task in for loop, because in the function executeTask() m.top.fileName will always be equal to the last i-number of the loop. So how I can run one task multiple times in for loop with unique data for each iteration?
if you're running them simultaneously I believe you need to use distinct tasks; create a new task with a distinct ID each time through. If you're wanting to re-use the same task then you probably don't want run the task within the for-loop, or don't use a for-loop and come up with a way that each new task is triggered when the previous completes. This is probably preferable from a "don't overload bandwidth and or roku processor with a bunch of simultaneous requests" standpoint.
The problem with the for loop now is that it modifies the task (repeatedly) before it completes, and the task has to call a function outside of the for loop upon completion. So you could have a bunch of distinct tasks running in parallel, or re-use the same task but do so sequentially, after each run completes, then modify and call the next task. keep a counter of completed tasks so when the last one completes it knows not to call any more.
m.tasksToLoad = albums.Count() m.counter = 0
then in the callback
m.counter++ if m.counter < m.tasksToLoad then launchNextTask()