Hello,
Thanks to jim_m_58 thread about background activity (this -very nice- one: https://forums.garmin.com/developer/connect-iq/f/discussion/5287/very-simple-sample-of-a-watch-face-with-a-background-process ) I added some Sun Events and Weather calculation to my watch face, all calculated only one to four times an hour rather than each second (!)
BTW it sometimes happens that no data is available and I don't know why / in what circumstances. Here is the ServiceDelegate class:
function onTemporalEvent() {
var data = new[5];
/! Weather data calculation
if (Weather.getHourlyForecast() != null) {
data[0] /* weatherIcon */ = whatIcon(Weather.getHourlyForecast()[2].condition);
(...)
}
else {
data[0] /* weatherIcon */ = "@";
(...)
}
//! Next sun event calculation
data[3] = getSunValue();
//System.println(sunEventHour.substring(0,2));
if (data[3] != "n/a") {
if (data[3].substring(0,2).toNumber() < 12) { //set sunrise icon
data[4] /* sunEventIcon */ = ">";
}
else { // set sunset icon
data[4] /* sunEventIcon */ = "?";
}
}
Background.exit(data);
}
And here is the App part:
function onBackgroundData(data) {
// Saving background data
if (data[0] != null) {
weatherIcon = data[0];
Application.getApp().setProperty("weatherIcon",data[0]);
}
// Else reading cached data
else {
if (Application.getApp().getProperty("weatherIcon") != null) {
weatherIcon = Application.getApp().getProperty("weatherIcon");
}
// Else default value
else {
weatherIcon = "@";
}
}
(...)
WatchUi.requestUpdate();
}
In the View part I use weatherIcon (and other set in App) variable to display data. This variable is global and initialized with "". Is there something I am doing in a wrong way? Thanks for your help