In my application view (watch app, not a watch face) I want to show current altitude with changes as they happen.
I'm planning to initially call Sensor.getInfo()
in onShow() method followed by Sensor.enableSensorEvents(callback)
and then call Sensor.enableSensorEvents(null)
in onHide()
:
hidden var _altitude as Float or Null;
function onShow() as Void {
_altitude = Sensor.getInfo().altitude;
Sensor.enableSensorEvents(method(:onSensorData));
}
function onHide() as Void {
Sensor.enableSensorEvents(null);
}
function onSensorData(sensorInfo as Sensor.Info) as Void {
_altitude = sensorInfo.altitude;
WatchUi.requestUpdate();
}
function onUpdate(dc as Graphics.Dc) as Void {
View.onUpdate(dc);
if (_altitude != null) {
_labelAltitude.setText(_altitude.format("%.0f"));
} else {
_labelAltitude.setText("N/A");
}
}
A few questions:
- Is
Sensor.getInfo()
considered blocking or not? Does it always instantly return currently known values or would it block until watch receives the data? is it OK to call it in onShow() method? - Do I need to call
label.setText()
inonUpdate()
or I can call it any time (e.g. in onShow or onSensorData) andView.onUpdate(dc)
will redraw the view with the newly set value? - Should I call
setText()
beforeView.onUpdate(dc)
or after? - Do you have any other recommendations to my current approach to make it most fluent and computationally efficient (recommended practice)?