AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
978 stars 75 forks source link

Nil in AutoInject properties in every VC that not in first uiContainer #185

Closed abesmon closed 5 years ago

abesmon commented 6 years ago

Code to reproduce

        class BaseVC: UIViewController {}

        class FooVC: BaseVC {}
        class FooOneVC: FooVC, StoryboardInstantiatable {
            private(set) var nest = Injected<Nest>()
            class Nest {}
        }
        class FooSecVC: FooOneVC {}

        class BarVC: BaseVC {}
        class BarOneVC: BarVC {
            private(set) var nest = Injected<Nest>()
            class Nest {}
        }
        class BarSecVC: BarOneVC {}

        // MARK: foo part
        let fooUICont = DependencyContainer { container in
            container.register(.shared) { FooOneVC() }
            container.register(.shared) { FooSecVC() }
        }

        let fooDataCont = DependencyContainer { container in
            container.register(.shared) { (controller: FooOneVC) -> FooOneVC.Nest in
                return FooOneVC.Nest()
            }
        }

        fooUICont.collaborate(with: fooDataCont)
        fooDataCont.collaborate(with: fooUICont)

        try! fooUICont.bootstrap()
        try! fooDataCont.bootstrap()
        DependencyContainer.uiContainers.append(fooUICont)

        // MARK: bar part
        let barUICont = DependencyContainer { container in
            container.register(.shared) { BarOneVC() }
            container.register(.shared) { BarSecVC() }
        }

        let barDataCont = DependencyContainer { container in
            container.register(.shared) { (controller: BarOneVC) -> BarOneVC.Nest in
                return BarOneVC.Nest()
            }
        }

        barUICont.collaborate(with: barDataCont)
        barDataCont.collaborate(with: barUICont)

        try! barUICont.bootstrap()
        try! barDataCont.bootstrap()
        DependencyContainer.uiContainers.append(barUICont)

        let fooSecVC = FooSecVC()
        fooSecVC.setValue(nil, forKey: "dipTag")
        print(fooSecVC.nest.value)

        let barSecVC = BarSecVC()
        barSecVC.setValue(nil, forKey: "dipTag")
        print(barSecVC.nest.value)

What was expected: fooSecVC.nest.value and barSecVC.nest.value would be not nil

What happens: fooSecVC.nest.value not nil, but barSecVC.nest.value is nil. And as seen in our main application, all VC's that are not in first uiContainer, works wrong with Injected<T>()

abesmon commented 6 years ago

Just to make it clear - in our main application we not working directly with setValue, we set it through Storyboards and result is the same

ilyapuchka commented 6 years ago

can you please submit pr with test that demonstrates the issue?

abesmon commented 6 years ago

sure, but i cant understand how to do it properly, because my test case use Dip-UI but Demo application not using it

ilyapuchka commented 6 years ago

you should do that in Dip-UI repo then

abesmon commented 6 years ago

ok. Should i close issue here?

ilyapuchka commented 6 years ago

leave it and just refer to it in the pr

abesmon commented 6 years ago

Done. Hope somebody can figure out what wrong happens =\

strange thing, that https://github.com/trimmurrti/Dip/commit/9e7bd51bcd86363638c29a777934e2188f021543 works well

jtwigg commented 6 years ago

@abesmon I'm running your branch: https://github.com/abesmon/dip-ui/tree/feature/several-containers-autoinject-fail

I can't get your test to pass even after @ilyapuchka reverted the #171 and Dip is upgraded to 6.1

It fails in all scenarios

The test appears to fail because at no point does BarSecVC ever implement StoryboardInstantiatable. When I add this to the test: class BarSecVC: BarOneVC,StoryboardInstantiatable {}

A) It passes with all of the Parent Child implementation (#171) b) It passes with Dip v6.1

@abesmon Would you please be able to produce a test that was a) Failing for 2cc5310d4bc6c1f42d79ff179c21da489988a0f3 (just prior to the (#171) revert b) passes v6.1? (revert #171)

john-twigg-ck commented 6 years ago

@ilyapuchka Still looking for a failing unit test. As stated above, I could not find an test that failed before and passed afterword.