Checking new data from remote server (slows down)

Hi everybody,

Here’s my “problem” : i have an app that checks every 15 sec (for example) for new data.
I’m using a script originally written by Gilbert Guerrero, so here’s the code :

[lua]local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
tickerTimer = {}

– local file size
local original_size = 0
– data link
local data_url = “http://somewhere.com/here.xml

local function checkNewData ()
local size2get = 0
local r, c, h = http.request {method = “HEAD”, url = data_url}

if c == 200 then
size2get = tonumber(h[“content-length”])
print( "Remote filesize : " … size2get )
connectionMade = true
else
print(“Error contacting remote host.”)
connectionMade = false
end
–compare the size of the files

if size2get ~= original_size and connectionMade then
local alert = native.showAlert( “New data !”, “yay !” )

original_size = size2get – Avoiding alert spam :wink:
end

end
function startTicker ()
print(“ticker starts”)
tickerTimer = timer.performWithDelay(5000, checkNewData,0)
end

function pauseTicker ()
print(“ticker is paused”)
timer.pause(tickerTimer)
end

function resumeTicker ()
print(“ticker is resumed”)
timer.resume(tickerTimer)
end

startTicker ()[/lua]

So tickerTimer works in background while the app is running.
It works pretty well, except it slows, even freezes the app… (Even the simulator!!)
Do you think there’s a better way to do this ?
Thanks for reading ! :wink:
(and i don’t want to use pubnub ) [import]uid: 3638 topic_id: 18017 reply_id: 318017[/import]

ok i’m using a different method with network.request it seems better! [import]uid: 3638 topic_id: 18017 reply_id: 68866[/import]

Pausing/resuming timers currently has some issues, which might contribute.

Have you viewed the Xcode terminal log while on device?

Peach :slight_smile:

PS - Printing mem usage may also help work out if there’s a problem there - try this code for that;

[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)[/lua] [import]uid: 52491 topic_id: 18017 reply_id: 68868[/import]

Hi, thanks i’m going to watch memory with your code.

My main issue appears while connecting (it caused somekind of “lag”, about 1 or 2 secs). Now it’s ok!

But that’s true, i’ve noticed some problems with timers… quite anoying :wink:

Anyway thanks a lot for your help ! [import]uid: 3638 topic_id: 18017 reply_id: 68874[/import]

Quick lazy fix! :wink:

[lua]function pauseTicker ()
print(“ticker is paused… em canceled!”)
–timer.pause(tickerTimer)
timer.cancel(tickerTimer)
end

function resumeTicker ()
print(“ticker is resumed… em restarted!”)
–timer.resume(tickerTimer)
tickerTimer = timer.performWithDelay(5000, checkNewData,0 )
end[/lua] [import]uid: 3638 topic_id: 18017 reply_id: 68882[/import]

Glad to hear it seems better :slight_smile: (And good lazy fix ;))

Peach [import]uid: 52491 topic_id: 18017 reply_id: 68885[/import]