Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwinjectStoryboard resolves a new instance of View Controller without dependencies #152

Open
gregoripolak opened this issue Nov 26, 2020 · 2 comments

Comments

@gregoripolak
Copy link

I have found an issue with SwinjectStoryboard, I am trying to create two instances of the same view controller: I.E. Here are my dependencies:

container.register(MyViewModeling.self) { r in
    MyViewModel(someDependency)
}
    
container.storyboardInitCompleted(MyVC.self) { r, c in
    c.viewModel = r.resolve(MyViewModeling.self)!
}

Here is an example of how I resolve my instances:

let myVcs = [0,1].map { _ in storyboard.instantiateViewController(withIdentifier: MyVC.Self) }

The issue is that the second instance of MyVC created, doesn't contain a viewModel. Meaning I don't go through the dependency injection process(invoke).

I started investigating, and as it turns out from what I see in SwinjectStoryboard source code, when I get to the actual resolve method, I hit the entry.storage.instance(inGraph: currentObjectGraph), which does find an entry for my VC, and instead of invoking dependency injection, it just returns the VC that I have already instantiated.

@gregoripolak
Copy link
Author

Anyone?

@teejayhh
Copy link

Give this a try perhaps.

container.storyboardInitCompleted(MyVC.self) { r, c in
    c.viewModel = r.resolve(MyViewModeling.self)!
}.inObjectScope(.transient)

https://github.com/Swinject/Swinject/blob/master/Documentation/ObjectScopes.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants