-
I have a question about how best to refer to another factory from a factory. Say I have something like this.
Inside of the recommendationsCoordinator factory I am referencing storage via self instead of making any assumptions about it being in Container.shared. I did this in case I do use a custom container down the line. However what I worry about is the explicit use of self in "self.storage()" capturing a circular reference and things not cleaning up property in the case of a move to a custom container. I realize that as long I use Container.shared this wouldn't be an issue. Is the circular reference a worry and if so what is the best way to handle the issue? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It's not a worry. I may need to document this more, but as mentioned in the readme Factories are transient; created when needed, used, and then quickly discarded. The definition of If that Factory is evaluated (Container.shared.recommnedationsCoordinator()), then the closure will be called, the parameter That Factory returns its service and is discarded, its job complete. The Everything's unwound, and the only thing now left is your reference to your coordinator in Just keep the temporal aspect in mind. The only thing you really shouldn't do it ask for a Factory and then stash it somewhere, as that Factory contains a hard reference to its container over its (usually brief) lifetime. |
Beta Was this translation helpful? Give feedback.
It's not a worry. I may need to document this more, but as mentioned in the readme Factories are transient; created when needed, used, and then quickly discarded.
The definition of
recommendationsCoordinator
is a computed value. Hence its reference to self doesn't really exist until it's actually called. In a similar vein, that reference to the container in{ RecommendationsCoordinator(storage: self.storage()) }
isn't captured until the Factory forrecommendationsCoordinator
is created and returned.If that Factory is evaluated (Container.shared.recommnedationsCoordinator()), then the closure will be called, the parameter
self.storage()
will be evaluated,.and the process starts over, recu…