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: 
marcotvapp
Level 7

ReplaceAll function takes quite a while

Hi everyone,
I am building an App which has few categories of videos. For each videos i need to save the id which is a huge number(integer) and to save it properly i need to save as string as shown below:

regex = CreateObject("roRegex", "Smiley Sad\s*)([0-9]{9,})", "")
safeResult = regex.ReplaceAll(jsonstring ,":\1" + Chr(34) + "\2" + Chr(34))
result = ParseJson(safeResult)


Sometimes ReplaceAll function takes quite a while and the user is not allowed to navigate through the app.
I said sometimes because on the same "jsonstring" sometimes works fine and sometimes not, what can I do to improve this issue?


Thanks,
Marco
0 Kudos
8 Replies
EnTerr
Level 8

Re: ReplaceAll function takes quite a while

Let me see if i get this right -
your app in BS makes JSON call, receives list of videos where some values are BIGNUMs and if you do ParseJSON() outright, those numbers get shoved into float and lose precision, i.e. unusable further as IDs?

An ultimate solution would be if RokuCo changes ParseJSON() to generate double instead of float for big numbers - that will increase precision from 24 to 53 bits and cover most all practical cases. I think we already discussed that somewhere. (PS. found it! it seems you are using TheEndless's trick even)

For you, the matter at hand - can you modify the server-side and make it send the IDs as strings and not long ints? Assuming your JSON string is really big, regex replace is bound to be slow.
0 Kudos
marcotvapp
Level 7

Re: ReplaceAll function takes quite a while

Hi EnTerr,
You understood perfectly my situation, but unfortunately i can not modify the server-side because i am using brightcove's api.
Also, I don't know if it's matter but the ReplaceAll function seems freeze if I am not using the app for a while and then try to change the content making another call :

               
data = m.data()
searchRequest = data.searchRequest
searchRequest.SetURL(url)
print " regex "
regex = data.regex
print "GETting SAFE RESULT for "regex
safeResult = regex.ReplaceAll(searchRequest.getToString(),":\1" + Chr(34) + "\2" + Chr(34))
print " LOADING..."
json = ParseJSON(safeResult)


m.data() is :

                
data : function ()
var = {
searchRequest : CreateObject("roUrlTransfer")
regex : CreateObject("roRegex", "Smiley Sad\s*)([0-9]{9,})", "")
}

return var

end function


On the console i can see "Getting Safe Result for <Component : roRegex " so the data should be initialized correctly..is not?
0 Kudos
RokuMarkn
Level 7

Re: ReplaceAll function takes quite a while

You are doing a GetToString and ReplaceAll in the same statement, so I don't see how you can tell which one is taking a long time. It seems much more likely that it's the GetToString that's taking a long time. You should separate the statements and confirm.

--Mark
0 Kudos
marcotvapp
Level 7

Re: ReplaceAll function takes quite a while

Hi Mark ,
thanks for your suggestion. I tried to separate the statements and actually you were right, the issue is on getToString Function, any suggestion to fix that?
0 Kudos
RokuMarkn
Level 7

Re: ReplaceAll function takes quite a while

Well, GetToString is reading data from the server, so its duration will depend on your local network conditions, your path to the server, and the server itself. How long is this taking when it takes a "long time"? It wouldn't be unreasonable for it to take a few seconds. The way to remove this delay from the user experience would be to use AsyncGetToString, although that will require some restructuring of your logic.

--Mark
0 Kudos
marcotvapp
Level 7

Re: ReplaceAll function takes quite a while

Hi Mark,
I am trying to use AsyncGetTostring as your suggestion as shown below :


searchRequest.setMessagePort(m.port)
searchRequest.SetURL(url)
timeout = 0
if ( searchRequest.AsyncGetToString())

while(true)

msg = wait (10,m.port)
print "typ msg "type(msg)
timeout = timeout + 10
if (type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if (code = 200)
search = searchRequest.getToString()
exit while
end if
else if (timeout = 5000 )
print "error"
return invalid
end if

end while

end if


Usually to get the issue discussed into previous messages, I need to leave the app for a while and then try to get new data, at this point getToStrings seems to freeze the app. What I discovered with the above piece of code is that the app doesn't freeze, type(msg) has invalid value instead. So, now, I can handle this case getting an error message after few seconds. Do you think is a good way to manage this issue ? What could be the reason for which only when I don't use the app for a while I get type(msg) as invalid ?
Also, currently the app shows an error when the timeout is expired and if the user try to get again new data the app works fine.

Thanks,

Marco
0 Kudos
RokuMarkn
Level 7

Re: ReplaceAll function takes quite a while

A few points:
1. Getting an invalid msg is normal when you have a timeout on your wait. It just means the timeout expired. Generally you should test type(msg) and only process messages of the type you care about. You should just ignore messages of other types (including invalid). This is a very important principle which should be used in all event loops.

2. When you receive roUrlEvent, you should call msg.GetString() to get the string, not call searchRequest.GetToString. GetToString will just issue a new request and wait for it. msg.GetString() will return the string from the async request that just completed.

3. Make sure you exit your loop even if the response code is not 200. You'll only get one roUrlEvent, whether the server returns 200 or something else.

4. It's better to use an roTimespan to check for your 5 second timeout, rather than maintaining your own counter. The wait may take slightly more or slightly less than 10 ms, and the inaccuracies could add up significantly.

5. Finally, when you get all these fixed, you'll have a piece of code that behaves just like GetToString(). To actually take advantage of the asynchronous nature of the calls, you should integrate the handling of the roUrlEvent message into your main loop, rather than having a separate loop that just waits for the UrlTransfer to finish. By putting the handling in your main loop, you can process other events, such as user actions, while you're waiting for the transfer to finish. This is what I meant earlier by "restructuring your logic".

--Mark
0 Kudos
marcotvapp
Level 7

Re: ReplaceAll function takes quite a while

"RokuMarkn" wrote:

1. Getting an invalid msg is normal when you have a timeout on your wait. It just means the timeout expired. Generally you should test type(msg) and only process messages of the type you care about. You should just ignore messages of other types (including invalid). This is a very important principle which should be used in all event loops.


I know that get an invalid msg is normal but i can't understand why when i leave the app for about a minute the call gives always an invalid msg. Also, after showing an error message where the user can retry to get data, the call works fine ( I am always using the same function )

I have understood the other points and I fixed as suggested
Thanks,

Marco
0 Kudos