Closed rmickeyd closed 10 months ago
Sigh. There are a lot of sides to this issue, including a previous one (#114), but you're right. Trashing the scope every time the Factory is resolved is not a good thing. :(
I removed the code that automatically clears the cache when setting a context and it's up in develop if you want to take a look.
A side effect of this is that if we ever want to change a Factory's context--but that Factory defines a scope--then we're also going to need to manually clear the scope cache for that object.
Container.shared.myService
.onTest { NullAnalyticsEngine() }
.reset(.scope)
I'm probably going to recommend that any context modifiers one wants to define should probably be in the autoRegister function as there's fewer side effects when you do so.
Makes sense to me. I agree about autoregister, that is the option I decided to move forward with.
The
onPreview
modifier seems to break the shared scope. Looking at the example below, when theonPreview
modifier is commented out theDefaultManager
will init exactly once and the same instance will be used in both theTestViewModel
andDetailViewModel
. When it is uncommented, theDefaultManager
will be initialized in theTestViewModel
and then a second instance will init/deinit with every push/pop of theDetailView
.I also tested this with other modifiers and the results seem to be consistent.
There does seem to be a couple of options to get around this but I still think the above should work.
Option 1 - Autoregister:
Option 2 - once() modifier: