The OnUpdate() method of an App's view is only called when WatchUi.requestUpdate() is called. Is there anyway to call the onUpdate() every second just like in a data field?
The OnUpdate() method of an App's view is only called when WatchUi.requestUpdate() is called. Is there anyway to call the onUpdate() every second just like in a data field?
You can use timer to trigger requestUpdate periodically:
using Toybox.WatchUi; using Toybox.Timer; var myTimer; function setupTimer() { myTimer = new Timer.Timer(); myTimer.start(method…
You can also do it based on another callback other than a timer.
Let's say you have GPS running:
Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
You can do the Ui…
You can use timer to trigger requestUpdate periodically:
using Toybox.WatchUi; using Toybox.Timer; var myTimer; function setupTimer() { myTimer = new Timer.Timer(); myTimer.start(method(:doUpdate), 1000, true); } function doUpdate() { WatchUi.requestUpdate(); }
This is repeating timer (last parameter true) every second. Or set it to false and restart it in doUpdate or where ever necessary. I made myTimer as global variable (can be class member too), so you keep a handle to the timer and can also stop it if needed (or change repeating time).
Just to note, system will also call onUpdate() occasionally, so don't rely on getting it called only once per second, it might come more often too.
More info:
You can also do it based on another callback other than a timer.
Let's say you have GPS running:
Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
You can do the Ui.requestUpdate() in onPosition.
Same with
Sensor.enableSensorEvents(method(:onSensor));
If you're doing a makeWebRequest, you can do in in the callback when you get the data/an error
it kind of depends on what your app does,
Another thing to note is watchfaces are different. There, the only place I use Ui.requestUpdate() is in the onBackgroundData callback for a background service.
Also, the Ui.requestUpdate() is ignored in widget glance views on some devices. The non pro f6 devices for example.
Thank you, that works fine.