Newbie question on background resources

Former Member
Former Member

Hey!

Im sorry if this information already exists in the toybox API or in already made question forums!

I was just wondering how the resources work with backgrounds. I tried understanding it more studying the samples given. In BackgroundTimerBackground.mc in the backgroundTimer sample there is the function that does following:

function onTemporalEvent() {

    // Use background resources if they are available
    if (Application has :loadResource) {
        Background.requestApplicationWake(Application.loadResource(Rez.Strings.TimerExpired));
    } else {
        Background.requestApplicationWake("Your timer has expired!");
    }

    // Write to Storage, this will trigger onStorageChanged() method in foreground app
    Storage.setValue("1", 1);

    Background.exit(true);
}

So my question when studying the code, specifically "if(Application has :loadResource)"- How often does a application get the resource (So when will the if-statement hold)? For how long? Can only one application at time get the resource? Just generally how does the background "resource-system" work?


Thanks in advance! Slight smile

  • The if is safeguarding against earlier versions of CIQ where loadResource might not be available. 

    Surprised to see Storage.setValue in background code, though!

     I thought that was explicitly illegal in background processes. 

  • So my question when studying the code, specifically "if(Application has :loadResource)"- How often does a application get the resource (So when will the if-statement hold)? For how long? Can only one application at time get the resource? Just generally how does the background "resource-system" work?

    The code (and comment) is actually referring to the fact that Application.loadResource() is only available starting with CIQ 3.1.0, and it's the only way for a background process to load a resource. So it's not a question of how often or when, but for which devices.

    For context, the standard way of loading resources is WatchUi.loadResource() (available since 1.0.0), but that's not available for background processes.

    So if a background process needs to load a resource, it's only possible with Application.loadResource(), which is only available for devices with CIQ 3.1.0 and higher.

    See:

    https://forums.garmin.com/developer/connect-iq/f/discussion/6460/cannot-load-resources-in-background-process

    https://developer.garmin.com/connect-iq/api-docs/Toybox/WatchUi.html#loadResource-instance_function

    developer.garmin.com/.../Application.html

  • When youing resources in the background they must be scoped for the background.

    See https://developer.garmin.com/connect-iq/core-topics/resources/  "Resource Scopes"

  • I'm kind of unclear on what you're doing some things the way they are.  You can avoid the 3.1 issue by not loading a resourse in the background (return an error code), and also not trying to do the setValue() for storage.  Return what's needed in Background.exit() and in onBackgroundData(), do what's needed as far as Storage or resources..

  • I'm kind of unclear on what you're doing some things the way they are.  You can avoid the 3.1 issue by not loading a resourse in the background (return an error code), and also not trying to do the setValue() for storage.  Return what's needed in Background.exit() and in onBackgroundData(), do what's needed as far as Storage or resources..

    The OP mentioned that the code is from a Garmin SDK sample and they're trying to understand the "if(Application has :loadResource)" part.

    I was just wondering how the resources work with backgrounds. I tried understanding it more studying the samples given. In BackgroundTimerBackground.mc in the backgroundTimer sample there is the function that does following:
  • Former Member
    0 Former Member over 3 years ago in reply to FlowState

    Hey Flowstate, thank you very much for the help. I have another question regarding background services. I have been trying to implement a test background service on my smartwatch now, but it does not seem to work for me.

    The code is quite simple:

    using Toybox.Application;
    using Toybox.WatchUi;
    using Toybox.Background;
    using Toybox.Time;
    
    (:background)
    class overloadApp extends Application.AppBase
    {
        var mView;
        var mBackgroundData;
    	
        function initialize() {
            AppBase.initialize();
            if(Background.getTemporalEventRegisteredTime() != null) {
                Background.registerForTemporalEvent(new Time.Duration(5*61));
            }
        }
    
        function onBackgroundData(data) {
            System.println("background data");
        }
    
        function getInitialView() {
        	System.println("App started");
        	mView = new overloadView();
            return [mView];
        }
    
        function getServiceDelegate(){
            return [new overloadDelegate()];
        }
    }
    
    ///////////////////////////////////////////////////////////////////
    
    using Toybox.Background;
    using Toybox.Communications;
    using Toybox.System;
    
    (:background)
    class overloadDelegate extends System.ServiceDelegate {
    	function initialize() {
            ServiceDelegate.initialize();
        }
    
    
        function onTemporalEvent() {
            System.println("In temporal");
            Background.exit(true);
        }
    }

    So basically what i want to see is if the System.println("background data") or System.println("In temporal") is written in the log-file after 5-6 min which i defined in initialize. I tried running the code and waiting for about 15 minutes, but nothing seems to happen. I could at least see "App started" which is the system.print in getInitialView when checked the log-file. 

    Thanks in advance! :) 

  • You probably don't want to do the register in initialize.  That gets called both when the main app starts, but also when the background starts.  Try moving the register inside getInitialView()

  • Former Member
    0 Former Member over 3 years ago in reply to jim_m_58

    Ooh, that seems to solve it! Thank you Slight smile