Search code examples
rokubrightscript

Is it possible to create multiple tasks with same function registering for observeField in brightscript?


We created task to receive the Program Information from server and to process the data. Once processing of the data is completed, the task is notifying by setting the response field and the screen is getting notified as it registered with observeField for the response as below.

Screen:

m.FetchSchedule = CreateObject("roSGNode", "FetchScheduleData")
m.FetchSchedule.scheduleRequestContent = channels
m.FetchSchedule.observeField("scheduleContent", "didReceiveInitialScheduleData")
m.FetchSchedule.control = "RUN"

Observer function in Screen:

sub didReceiveInitialScheduleData()
        if m.FetchSchedule.scheduleContent <> invalid and m.FetchSchedule.scheduleContent.getChildCount() > 0 and m.FetchSchedule.totalRows > 0
            ? "GuideScreen.brs - [didReceiveInitialScheduleData]"
    end if
end sub

This is working fine with out any issues for single task. But we need to get program information of channels of all categories. Now we are doing this sequentially means once the task is completed for one category, we are creating another task for another category. This is working fine but it is taking time as we are doing it sequentially.

To reduce the delay, we are trying to create one task for each category and trying to run simultaneously. But not able to think how to modify the observer function.

Code to create multiple tasks in Screen:

for x = 0 to m.global.AllNeighborhood.count() - 1
    m.FetchSchedule[x] = CreateObject("roSGNode", "FetchScheduleData")
    m.FetchSchedule[x].scheduleRequestContent = channels
    m.FetchSchedule[x].categoryIndex = x
    m.FetchSchedule[x].observeField("scheduleContent", "didReceiveInitialScheduleData")
    m.FetchSchedule[x].control = "RUN"
end for

We are passing the thread index as 'categoryIndex'. But unable to modify the observer function "didReceiveInitialScheduleData". Because we need the index from observer function to access the response. Even though we are saving the index as 'categoryIndex', even to access that, we need index. We got stuck here.

Can anyone please let us know whether it is possible to create multiple tasks with same observer function. If possible, how we can know from which thread, we got the response. If not possible, is there anyway to fix this issue.


Solution

  • By specifying an argument in your observer callback function you can access the node that triggered the event.

    sub didReceiveInitialScheduleData(message as Object)
        task = message.getRoSGNode()
        ? task.scheduleContent 'This will print the respective task's scheduleContent field
        ...
     end sub
    

    This way, whenever the callback function is called you know exactly which task triggered it and can easily access its response.