unmap a singleton in robotlegs 1.5.2

itsmylifesoham's Avatar

itsmylifesoham

29 Mar, 2012 03:06 PM

hi,

just want to be sure if in 1.5.2 version RL when we do injector.unmap(some singleton) does it get removed immediately?
or do i have to use the swift suspenders 2.0 like its mentioned in other threads for RL version 1.0 ?

another thing. i want to unmap a singleton to release it for sometime. then later when the singleton interface is injected somewhere i want it to get injected again.

so should i be doing injector.unmap() and then immediately calling injector.map() ??
will this create two instances of the single ton?

thanks :)

  1. Support Staff 1 Posted by Shaun Smith on 29 Mar, 2012 10:21 PM

    Shaun Smith's Avatar

    Hello!

    unmap() simply removes the mapping. It will not null out any existing references to injected instances. If you create a new mapping, a new instance will be used for future injections.

  2. 2 Posted by itsmylifesoham on 30 Mar, 2012 05:06 AM

    itsmylifesoham's Avatar

    hi,

    i have a simple scenario. i have some services (singletons) for my game lobby. when the game starts i want to remove these singleton services to free up memory. but on returning back from the game to the lobby i want a new instance of this service to be created. so i was hoping on gamestart i will do injector.unmap() so that the service singleton gets removed. i understand that if i am holding a reference to that singleton elsewhere it will not be removed. but i can make sure of that. just want to know how to make sure that robotlegs loses a reference to that singleton so that it may free up space.

  3. Support Staff 3 Posted by Shaun Smith on 30 Mar, 2012 03:20 PM

    Shaun Smith's Avatar

    In that case, you are correct. Calling unmap will release the cached singleton instance. As long as you've cleaned up all you're own references, the singleton instance should be free for garbage collection. Later, when you recreate the mapping, a new instance will be created and cached for future injections. Hope that helps!

  4. 4 Posted by itsmylifesoham on 30 Mar, 2012 06:18 PM

    itsmylifesoham's Avatar

    yea that clears it.
    so just so i understand correctly ..should i be calling injector.map() immediately after calling unmap() ?
    i hope that the new instance created and cached for future injections will not be active or eat memory until it is injected after coming back from lobby? am i right?

  5. Support Staff 5 Posted by Shaun Smith on 30 Mar, 2012 06:23 PM

    Shaun Smith's Avatar

    Yep, mappings do not eagerly create instances, so the new instance will only be created on the next injection.

  6. 6 Posted by itsmylifesoham on 31 Mar, 2012 10:07 AM

    itsmylifesoham's Avatar

    thanks a lot! :)

  7. itsmylifesoham closed this discussion on 31 Mar, 2012 10:07 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac