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

Deallocated variable when trying to resolve object #516

Open
EgorkZe opened this issue Aug 20, 2022 · 1 comment
Open

Deallocated variable when trying to resolve object #516

EgorkZe opened this issue Aug 20, 2022 · 1 comment

Comments

@EgorkZe
Copy link

EgorkZe commented Aug 20, 2022

Im trying to resolve viewModel in my MVVM architecture

My assembly class

public final class CancelAssembly: Assembly {
    
    public init() {
        
    }
    
    var container: Swinject.Container?
    var viewController: UIViewController!
    
    public func assemble(container: Swinject.Container) {
        container.register(CancelCoordinatorProtocol.self,
                           factory: {(r, assembler: Assembler?, rootViewController: UINavigationController, order: Order) -> CancelCoordinator in CancelCoordinator(rootNavigationController: rootViewController, assembler: assembler, order: order)
        }).inObjectScope(.transient)
        
        container.register(CancelViewControllerProtocol.self, factory: {(r, viewModel: CancelViewModel) -> CancelViewController in
            return CancelViewController(viewModel: viewModel)
        }).inObjectScope(.graph)
        
        container.register(CancelRouterProtocol.self, factory: {(r, routes: StrongRouter<CancelRoute>) -> CancelRouter in
            return CancelRouter(routes: routes)
        }).inObjectScope(.graph)
        
        container.register(CancelViewModelProtocol.self,
                           factory: {(r, router: StrongRouter<CancelRoute>, order: Order, step: Int) -> CancelViewModel in
            let router = r.resolve(CancelRouterProtocol.self, argument: router) as! CancelRouter
            let viewModel = CancelViewModel(router: router, order: order, step: step)
            let vc = r.resolve(CancelViewControllerProtocol.self, argument: viewModel) as? CancelViewController
            viewModel.viewController = vc
            return viewModel
        }).inObjectScope(.graph)
        
    }

}

Trying to resolve this way

let vm = assembler?.resolver.resolve(CancelViewModelProtocol.self, arguments: strongRouter, order, index) as? CancelViewModel

When I resolve it first time all is working correctly
But second time it gives me nil in viewModel.viewController
I checked that my CancelViewController deinited after 'return viewModel', but in first time it is ok.

viewModel has weak reference to viewController
and viewController has strong reference to viewModel

What am I doing wrong?

@welshm
Copy link
Contributor

welshm commented Sep 13, 2022

Nothing has a strong reference to the view model - I think if you want it to persist beyond that scope you'll need a strong reference (somewhere).

I think it's more common that the VC has the model injected into it (as opposed to the other way around) so that you then use the VC by inserting it into the navigation, and creating a strong reference that way

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