Are Services destroyed when not used anymore
Ok I have a service that its only task is to load an xml, transfert the data in a ValueObject and send that ValueObject via an event. I need that service to do its task at startup and I need it to be destroyed after.
What I understood is that if I use injector.mapSingleton(XmlService), it will not be instantiated at that moment so I need to do that myself and pass it the eventDispatcher because it will not be injected:
var xmlService:XmlService = new XmlService();
xmlService.eventDispatcher = eventDispatcher;
I do this in my CreateModelsCommand.
With that in mind, here are my questions:
If this service will not be injected anywhere do I need to do
this injector.mapValue(XmlService, xmlService)?
Since there are no reference to the service, don't I run the risk
of it being Garbage Collected before it has done it's task?
How do I destroy the service when it is done?
Thank you for your help
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
Support Staff 1 Posted by Till Schneidere... on Feb 18, 2010 @ 05:01 PM
The service is indeed very likely to be garbage collected at a random
point in time if that's all you're doing with it.
What you'd normally do is the following:
- map the service (either with mapSingleton or with mapValue after
manually creating it)
You can also use the injector to instantiate the service like this:
var xmlService:XmlService = injector.instantiate(XmlService);
If you add [Inject] metadata to the eventDispatcher property, it will
be injected automatically this way.
- map a command to the STARTUP command that instructs the service to load data
- map a command the your service's complete event
- in this event, call injector.unmap(XmlService) to remove the mapping
This will keep your service in memory as long as it's needed, allow
for injecting it into the command that's used to apply its result and
will clean it up afterwards.
There's a catch, though: The bundled DI solution, SwiftSuspenders,
currently doesn't remove singleton instances when you call unmap. If
you want to make sure that the service really gets collected, you must
therefore use mapValue.
Support Staff 2 Posted by Joel Hooks on Feb 18, 2010 @ 05:19 PM
On Feb 18, 2010, at 11:01 AM, tschneidereit wrote:
> There's a catch, though: The bundled DI solution, SwiftSuspenders,
> currently doesn't remove singleton instances when you call unmap. If
> you want to make sure that the service really gets collected, you must
> therefore use mapValue.
Is that a bug or feature? ;)
Support Staff 3 Posted by Till Schneidere... on Feb 18, 2010 @ 05:22 PM
I'd say it's a bug that might come in handy on very few occasions ;)
In any case, I've just created a ticket for it:
http://github.com/tschneidereit/SwiftSuspenders/issues/issue/19
4 Posted by zedia.net on Feb 18, 2010 @ 06:41 PM
Thank you that answered my questions
Till Schneidereit closed this discussion on Feb 18, 2010 @ 06:44 PM.