Beginner: how to use data storage

Former Member
Former Member

I want to make a custom odometer data field for Edge devices. My motivation is twofold:

1) the current odometer is not uniform across activity profiles. I have several cycling profiles and each has its own totals. I want proper total distance

2) There is no easy way to offset the distance on Garmin Express.

What I basically want to do is have a float that is stored as a property. The current distance then gets added to it. This should be quite simple.

However, learning how to use storage is quite confusing. 

1) How do I create a resource file and how do I type in the properties and settings? I just need one single number that can be altered in Garmin expres

2) How do I access it? Do I need to create an app object or can I just use Application.getApp().getValue() and ~setValue() respectively?

I would greatly appreciate any help/outline of the steps I need to follow in order. And please, don't reference the examples from the SDK, they seem too complicatd (and I already made 2 data fields without understanding them, so :p )

Many thanks!

  • Ok, There are properties(settings) and storage, and they are two different things, and there;'s also two ways to access both.

    The old way is

    Application.getApp().getProperty() and Application.getApp().setProperty().

    These is used for both properties and the ObjectStore, and which is used depends on if you have the property defined in the xml file.

    With a property in the xml, it's a property (the settings/<app>.set file), and is generally used in conjunction to a setting, so users can access/change (settings on your phone or with Garmin express.).  If you don't have the property in the xml file, it's for the ObjectStore, and it's local storage (the data/<app>.str file)

    The new way is

    Application.Property.getValue() and Application.Property.setValue() for properties

    and

    Application.Storage.getValue() and Application.Storage.setValue() for the object store

    The newer calls aren't available on older devices, but as you see, by their names make it easier to tell what it's doing.  With Application.Storage, you have much more space available (each key can be 8k instead of the total storage being 8k for example..)

    I use the object store for things that only the app itself will change, and the property only if there is an associated setting and the user can change

  • Former Member
    Former Member over 6 years ago in reply to jim_m_58

    thanks! ok, let's say I want to use the method. As the data field will change it during use, I assume it will have to be an object. But it should also have a parameter that is like the property you describe. 

    If I just type Application.Property.setValue() can it be accessed without further declarations? And that would mean it cannot be accessed as a setting. What I also don't get is how do I modify the xml file to add a property/setting? How do I open it within eclipse?

  • Take a look at some of the samples in the SDK.  ApplicationStorage and the ObjectStore sample..

    The ObjectStore sample shows the old way to access the object store as well as how to do app settings.

    The ApplicationStorage, how to access the object store with the new calls.

  • Former Member
    Former Member over 6 years ago in reply to jim_m_58

    Ok, so I decided to use the properties and settings xml approach. However, I cannot understand where the value is stored. Here is the code:

    using Toybox.WatchUi;
    using Toybox.Application;
    
    class OdometerView extends WatchUi.SimpleDataField {
    
    		var offset = Application.getApp().getProperty("offset");
            var distance = 0;
    
        function initialize() 
        {
            SimpleDataField.initialize();
            label = "Odometer";
            
        }
    	function returnFormatted(float, string)
        {
        	 if(float == 0)
        	{
        	return "__" + string;
        	}
        	
        	
        	else if (float < 10.01)
        	{
        	return float.format("%01d") + string;
        	}
        	else if (float < 100.01)
        	{
        	return float.format("%02d") + string;
        	}
        	else if (float < 1000.01)
        	{
        	return float.format("%03d") + string;
        	}
        	else if (float < 10000.01)
        	{
        	return float.format("%04d") + string;
        	}
        	
        	else
        	{
        	return float.format("%05d") + string;
        	}
        	}
      
        function compute(info) 
        {
        	if (info.elapsedDistance != null)
        	{
        	distance = offset + info.elapsedDistance/1000;
        	Application.getApp().setProperty("offset", distance);
            return returnFormatted(distance, " km");
            }
            else
            {
            return returnFormatted(offset, " km");
            }
         
          	
        }
    
    }

    I created the xml file with the offset property/setting. However, when I run the simulator, the value does NOT get stored in that xml. I can't trace where it's going. Also, it is not retrieved reliably. What might i be doing wrong?

  • With the property/settings, you can look/change them with the app settings editor in Eclipse.

    If you use that to change things while you are running, you may want to use onSettingsChanged in your AppBase to be notified when things change, so things like your "offset" can be set to the correct value.