Code optimization.

Good day to all.

Tying to optimize code of WF to reduce delay of start (return back to) WS, same for leaving WF to some other place (setting menu, app menu or widget list). Delay abt 3 sec for both directions.
I have some functions which has to be refreshed once per 24 hrs and store received data to “App.Storage” for further usage.
To avoid empty values on initial launch of WF, I’ve made initial call for them in “initialize” function of “View” class. But it calls for delay on start/return to WF. So, I found the way how to remove them from there. Now WF starts normally.

Refresh call for same functions in functions “onEnterSleep” and “onExitSleep” of “view” class. To keep code of those functions inside of “onUpdate” function – useless wastage of battery and memory, due to they have to called once per 24 hours.

Are there any ideas, where to move those functions from “onEnterSleep” and “onExitSleep”? Or somehow to rearrange code, to speed-up exit from WF?
  • I'm not quite sure what you are considering, as with all my onEnterSleep() and onExitSleep() functions, I have maybe 2 lines of code, as they are just to let the WF know if onUpdate will be called every minute or every second.

    In each function I set a boolean to know what mode I'm in (if seconds should be displayed or not) and call Ui.requestUpdate() to refresh the screen if seconds are currently being displayed)

    If you are doing much more (like your 24hr things), you probably want to do that in onUpdate, as onUpdate had the info it needs to see if the 24hr stuff needs to be done. (Date and time)
    function onUpdate(dc) {
    ...
    if(thisDay!=lastDay) {
    lastday=thisDay;
    //do 24 hr things
    }


    Another thing to consider, is maybe only do that check when you are in lowpower mode (when onUpdate is only called every minute). When switching to a widget, it could be the WF sees that as a gesture, and you're doing this in highpower mode, when the watchface is exiting)
  • Former Member
    Former Member over 7 years ago
    How many elements are you storing in application storage? This storage backend reads and writes the disk directly and encrypts/decrypts data, so it is expensive. If you have lots of individual records, you can probably get a significant speed up by keeping them in a dictionary, and saving/restoring the entire set of data with a single getValue call when your app loads.
  • Gentlemen, here link to my wf, source also posted there.
    https://apps.garmin.com/en-US/apps/9d8c51bb-f399-4f55-a265-b10350cab801#0
    Source:
    https://www.dropbox.com/sh/yqvoamkvjunsbsu/AABWXmGgnn2zUPefB6sSkAV0a?dl=0

    From the begin, I tried to avoid pass through the code every second (or minute, while at sleep mode), if I need to update data every 24 hrs, so I put them for refresh the data when wf coming into / passing out sleep mode. In same time, I did not want to see empty screen when wf launched firstly, so I’ve made call all those functions in initialize part of view class.
    Function like: calculation of sunrise/sunset, moon phase, sun elliptical points passage. After fw 9.2/9.53/9.71 with disaster battery leakage, I’ve drop there also number of week in a year, even transfer of month and week day to full stile.
    But got other story, to get out from wf to app menu device spend about 6 second… to show wf back – less but not so much… for mow, I removed all calls for functions in initialization part of view class, as result (looks like) return back to wf from other (any) please coming faster. Now looking for possibility to clear onEnterSleep and onExitSleep in view class.
    To get more clear what do I mean, see source code.
  • I'm not quite sure what you are considering, as with all my onEnterSleep() and onExitSleep() functions, I have maybe 2 lines of code, as they are just to let the WF know if onUpdate will be called every minute or every second.

    In each function I set a boolean to know what mode I'm in (if seconds should be displayed or not) and call Ui.requestUpdate() to refresh the screen if seconds are currently being displayed)

    If you are doing much more (like your 24hr things), you probably want to do that in onUpdate, as onUpdate had the info it needs to see if the 24hr stuff needs to be done. (Date and time)
    function onUpdate(dc) {
    ...
    if(thisDay!=lastDay) {
    lastday=thisDay;
    //do 24 hr things
    }


    Another thing to consider, is maybe only do that check when you are in lowpower mode (when onUpdate is only called every minute). When switching to a widget, it could be the WF sees that as a gesture, and you're doing this in highpower mode, when the watchface is exiting)



    Thanks for idea, Jim.
    I’ve used flag check (means, if data received before, for initial call) and time check (00.00.00 single call for every midnight) through “||”.
    All in inUpdate function. Others clear now.