I'm getting this assertion error when trying to resolve dependency. It not happens every time, so I cannot figure out how to reproduce it and what it can be...
I'm using a global Container variable to access dependencies and then I have another global variable which contains all necessary dependencies registrations:
var DIContainer = Container()
var initDIContainer : () = {
DIContainer.register(ReachabilityManagerType.self) { r in
AFNetworkReachabilityManager.shared()
}
.initCompleted { r, manager in
manager.startMonitoring()
}
.inObjectScope(.hierarchy)
// and so on...
}
In my custom main.swift file I have _ = initDIContainer to early initialize dependencies.
But then, at random circumstances I get assertion failure here:
fileprivate let userProfileManager = DIContainer.resolve(UserProfileManagerType.self)!
Any ideas about what it can be?
I'm not sure the cause of the problem, seeing the piece of code. Here are some hints.
resolve in a single thread?Hello @diwengrum. Did you find any clue to reproduce the issue? Replying to the questions 1, 2 and 3 is helpful to investigate the issue.
@yoichitgy, I'm using Async Display Kit for my UI, so there are some UI elements being allocated on a background thread and they're calling resolve method. So basically I think the problem is that dependencies are sometimes resolved before they're registered on main thread.
Did you use synchronize method for thread safety? If you use it, the problem might be resolved. Documentation is here:
https://github.com/Swinject/Swinject/blob/master/Documentation/ThreadSafety.md
@yoichgy, I'm using Async Display Kit for my UI, so there are some UI elements being allocated on a background thread and they're calling resolve method. So basically I think the problem is that dependencies are sometimes resolved before they're registered on main thread.
Most helpful comment
Did you use
synchronizemethod for thread safety? If you use it, the problem might be resolved. Documentation is here:https://github.com/Swinject/Swinject/blob/master/Documentation/ThreadSafety.md