Clearing Timer.Timer() object instance when onHide() is called during app screen navigation?

I am writing an app for the Edge1030 and running the Connect IQ Device Simulator 7.3.0.

Within two of my app views separate Timer.Timer() object is being used.  

I followed the demo app to set up a timer in one of my views.  Using three different timers and callbacks, similar to the demo app.  This crashed my app, which I believe is because a separate view is running a fourth timer, where my understanding is CIQ limits to max three timers at a time.

I have since reduced the on screen timer to a single instance of Timer.Timer() and have everything working.  So no problems here.

My understanding is that Timer.start(method(:callback1), 1000, true) and Timer.stop() will respectively start/stop a timer instance.  

However, I do not understand how to clear an instance of Timer.Timer() from memory so that I may use different timers elsewhere within the app.

Ideally, when onHide() is called within my view I wish to clear the instance of the applicable timer.  Is there a way to do this?  Or do I misunderstand and the instance of Timer.Timer() is cleared when Timer.stop() is called?

Top Replies

All Replies

  • However, I do not understand how to clear an instance of Timer.Timer() from memory so that I may use different timers elsewhere within the app.

    In Monkey C, an instance of any class (also known as an object), will be cleared from memory as soon as there are no references to the object. In other words, an object stays in memory as long as there is at least one reference to it. This form of memory management is known as reference counting.

    A reference to an object can be stored in a local variable, class member variable or global variable.

    Once a local variable goes out of scope (e.g. when the containing function returns), then that variable is destroyed, and it no longer counts as a reference to its value.

    In all cases, if a given variable references an object, and the same variable's value is changed to something else (such as null or a different object), then the previous object is no longer referenced.

    The most common way to ensure that an object is destroyed is to set all referencing variables to null (or to allow them to go out of scope, if they are local variables).

    e.g.

    var x;
    var y;
    //...
    x = new Timer.Timer(); // x references the new timer object (for a total of 1 reference)
    y = x; // y also references the timer (there are now 2 references)
    x = null; // x no longer references the timer (there is now 1 reference)
    y = null; // y no longer references the timer (there are now 0 references, and the timer object will be destroyed)

    In your case, you could create and destroy your timer in onShow() and onHide().

    Another approach could be to keep a single Timer.Timer() instance in the app class, and provide methods in the app class to access the timer. This is probably not the cleanest approach, though.

  • Thank you.  This is exactly the understanding I was looking for.

  • How is it possible to use "#" in "(timer # null)I"? Please explain!

  • how is it possible on the code section above with timers example it look like not equal symbol ""? 

  • how is it possible on the code section above with timers example it look like not equal symbol ""? 

    Sorry for the screenshot, the forum has been code blocking us for months now.

    The actual text is "timer != null". It's displayed in the screenshot as "timer null" due to a (somewhat controversial) coding font feature called ligatures.

    Personally I can see both sides of the argument. On the pro-ligature side, they look "cool" (or at least they did 5 years ago), and they arguably focus on the meaning of the code as opposed to the individual characters. On the anti-ligature side, they can be confusing and obscure meaning.

    https://www.google.com/search?q=ligatures+code

  • Outside of the context of coding, it's somewhat similar to how phones, Microsoft Word, and macOS will change straight quotes to curly quotes, and double hyphen ("--") to em dash, except ligatures are display only. (With ligatures, the underlying text is unchanged, it's only the display of the text in supported IDEs that is changed.)

    In the future I should probably disable ligatures before taking screenshots of code. However, when I take a screenshot of non-trivial code, I usually also include a link to the actual text on pastebin, where ligatures are not an issue.

  • The actual text is "timer != null". It's displayed in the screenshot as "timer null" due to a (somewhat controversial) coding font feature called ligatures.

    The forum won't let me edit the original comment, so here's my edited text:

    "The actual text is "timer != null". It's displayed in the screenshot as "timer null" due to a (somewhat controversial) coding font feature called ligatures, which combines multi-character coding symbols such as "!=" and "==" into a single character, when using a supported font (such as FiraCode or Cascadia Code) and app (such as VS Code, neovim, iTerm2 and Windows Terminal)."

    I'm sure the tide will eventually turn against ligatures, same as dark mode is already falling out of favour (in some cases).

    Playing with this feature in Windows Terminal (Command Prompt), it produces arguably incorrect behavior when you type "=" at the beginning of a command line, when using the (default) prompt that ends with ">": the ">" and "=" are combined into ≥ even though that's clearly not the meaning in that context.

  • Oh my… I’m too old for stuff like ligatures %). The case discussed above has really opened my eyes to these new inventions in the coding world. Thank you so much for such a detailed explanation, covering both the pros and cons with examples.

    As for ligatures in coding, my personal view is that, in general, it’s better to reduce the variety of characters used. For example, as a Ukrainian, I strongly support the idea of converting Ukrainian typing from Cyrillic to Latin characters. It’s better to use combinations of symbols for letters that don’t exist in the basic Latin charset—like Ї = JI.

    It seems we’re drifting a bit too far from the main topic in this discussion, though. Blush