Simulator hangs while running makeJsonRequest in timer callback.

Former Member
Former Member
Hello all.

I find that Simulator hangs after running makeJsonRequest recursively in the timer callback as shown in the following proof-of-concept code. The code is modified from the sample "Timer". I want to run makeJsonRequest every 10 sec to get data from a website. Is this the problem of the Simulator only? Since my device is not delivered yet, I wonder if the problem also exists on a real device. Any advice would be appreciated.

using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.Timer as Timer;
using Toybox.Communications as Comm;
using Toybox.System as Sys;

var timer1;


class Dat
{
var dat1;
var dat2;
var dat3;
}


class MyWatchView extends Ui.View
{

var dat = new Dat();

function callback1()
{

Comm.makeJsonRequest("json-time.appspot.com/time.json",
{}, {}, method(:onReceive));

}

function onReceive(responseCode, data)
{
if( responseCode == 200 )
{
dat.dat1 = data["hour"];
dat.dat2 = data["minute"];
dat.dat3 = data["second"];
Sys.println(dat.dat1+":"+dat.dat2+":"+dat.dat3);
}
else
{
Sys.println( "Failed to load\nError: " + responseCode.toString() );
}
Ui.requestUpdate();
}


function onLayout(dc)
{

Comm.makeJsonRequest("json-time.appspot.com/time.json",{}, {}, method(:onReceive));

timer1 = new Timer.Timer();
timer1.start( method(:callback1), 10000, true );

}

function onUpdate(dc)
{
var string;

dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
dc.clear();
dc.setColor( Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT );
string = "Time=" + dat.dat1 + ":" + dat.dat2 + ":" + dat.dat3;
dc.drawText( 40, (dc.getHeight() / 2) - 30, Gfx.FONT_MEDIUM, string, Gfx.TEXT_JUSTIFY_LEFT );
}

}

class InputDelegate extends Ui.BehaviorDelegate
{
function onMenu()
{
timer1.stop();
}
}



Best regards,
Anat
  • Couple of things here.

    1) The simulator itself will crash after 10 makeJsonRequests(). Even if you only make one call in the program, but run the program 10 times. You get a notice of the crash on windows, and not sure about macs. This is probably what you are seeing, as you do the makeJsonRequest() in the timer callback.

    2) With a real device, every 10 seconds is too quick, as the call itself (device->phone(bt)->internet and back can take almost 10 seconds, even with good comm)
  • Former Member
    Former Member over 10 years ago
    Couple of things here.

    1) The simulator itself will crash after 10 makeJsonRequests(). Even if you only make one call in the program, but run the program 10 times. You get a notice of the crash on windows, and not sure about macs.

    2) With a real device, every 10 seconds is too quick, as the call itself (device->phone(bt)->internet and back can take almost 10 seconds, even with good comm)

    3) just glancing at the code, the stuff you do in onLayout() is the stuff I do in initialize().



    Thanks for your reply.

    1. Would a real device have the 10-makeJsonRequest() limit?

    2. I am quite flexible in the time interval in the real device.

    I have another question.. Do I need to have Garmin Connect Mobile for iOS running in the foreground all the time while having the live communication? Can the phone sleep?

    Anat
  • No, there is no limit on the real device. It's a bug in the simulator.

    I don't have an iPhone, but GCM must be in the same state where you get things like notifications. On Android, it can be running in the background, but it must be running.
  • Former Member
    Former Member over 10 years ago
    Thanks for the clarification :)
  • that Simulator hangs after running makeJsonRequest recursively in the timer callback


    There is no recursion in your code.