Properties are sometimes lost

I do get the current GPS position like following inside my app inside the background callback and it worked for months without problems. Now, suddenly, I see problems and see that people have to activate GPS again every now and then because my watchface forgets the last known position.

Original Code

  • always use getProperty/setProperty on all devices
  • also persist 0 value

Question

How can this happen? For me it looks like my watchface should never forget a position (because it saves and retrieves the last known position if the system does not provide a new valid position)

UPDATE

Based on the feedback I got I made following adjustments:

  • only write/read values that are != 0 (as Fenix 6 devices seem to report 0 instead NULL if GPS location is unknown)
  • use Application.Storage on devices that do support them (as this way to store data is more reliable)

Final Code / Logic

  • I do use an in memory cache for the location - global variables inside my app class
  • inside my apps initialise code I call the Util.updateCurrentLocation() function
  • inside the watchfaces onExitSleep I do call the function again to eventually update the location
  • inside the drawing function I do read the values from the in memory cache

// --------------------
// function to update location in memory cache
// --------------------

static function updateCurrentLocation() {
    var location = Activity.getActivityInfo().currentLocation;
    if (location) {
        location = location.toDegrees();
        gLocationLat = location[0].toFloat();
        gLocationLng = location[1].toFloat();
        if (gLocationLat != null && gLocationLng != null && gLocationLat != 0 && gLocationLng != 0) {
            Application.Storage.setValue("Lat" /*Constants.PROP_LAST_LOC_LAT*/, gLocationLat);
            Application.Storage.setValue("Long" /*Constants.PROP_LAST_LOC_LONG*/,  gLocationLng);
        } else {
            gLocationLat = null;
            gLocationLng = null;
        }
    } else {
        var lat = Application.Storage.getValue("Lat" /*Constants.PROP_LAST_LOC_LAT*/);
        if (lat != null && lat != 0) {
            gLocationLat = lat;
        }

        var lng =  Application.Storage.getValue("Long" /*Constants.PROP_LAST_LOC_LONG*/);
        if (lng != null && lng != 0) {
            gLocationLng = lng;
        }
    }
}

// --------------------
// code to check if location is known or not
// --------------------

static function isLocationKnown() {
    if (gLocationLat == null || (gLocationLat == 0 && gLocationLng == 0)) {
        // GPS location is not known
        return false;
    }
    return true;
}