Open xrayman opened 5 years ago
Maybe try weakSingletone
scope?
weakSingleton could help, but I will need to retain somewhere instance myself. Well yes, I can find workaround for this. But I want to mention this issue to Dip developers. I think it should be fixed somehow.
@xrayman can you add a test that demonstrates this?
@ilyapuchka What do you mean by "test"? Test project?
no, unit test is enough
@ilyapuchka insert tests into DipTests.swift
// will pass
func testSingletonsInCollaborationUsingCollaborator() {
let collaborator = DependencyContainer()
collaborator.register(.singleton) { ResolvableService() as Service }
container.collaborate(with: collaborator)
weak var service = try? collaborator.resolve() as Service
XCTAssertNotNil(service)
collaborator.reset()
let noResolveContainer = try? container.resolve() as Service
let noResolveCollaborator = try? collaborator.resolve() as Service
XCTAssertNil(noResolveContainer)
XCTAssertNil(noResolveCollaborator)
XCTAssertNil(service)
}
// will fail
func testSingletonsInCollaborationUsingContainer() {
let collaborator = DependencyContainer()
collaborator.register(.singleton) { ResolvableService() as Service }
container.collaborate(with: collaborator)
weak var service = try? container.resolve() as Service
XCTAssertNotNil(service)
collaborator.reset()
let noResolveContainer = try? container.resolve() as Service
let noResolveCollaborator = try? collaborator.resolve() as Service
XCTAssertNil(noResolveContainer)
XCTAssertNil(noResolveCollaborator)
XCTAssertNil(service)
}
Hello! Here's the situation. I have some singletons which are make sense only if user is logged in. So after user is logged out I want to release all those singletons and recreate new instances after the next login. To do that I've separated all those singleton into another container and collaborate it with the main container. Then I register singletons on login and clear second container on logout. The problem is after I clear the second container singletons are not getting released because main container retains them (but I can't resolve them from main container). They are released only after next login when I register and resolve them again.
Is this an intended behavior?
Code example: