Description
In the app I am working on we often do registrations in Assemblies like so:
container.register(SomeType.self) {
SomeType(
dependencyOne: $0.resolve(Provider<DependencyOne>.self)!
)
}
This is however not thread safe, so we sometimes experience that when we call instance
on the Provider it will crash with a: If accessing container from multiple threads, make sure to use a synchronized resolver.
. As I can read from the code this is because some other resolve might have occurred simultaneously with this, and because the Resolver
isn't synchronised in the factory methods it isn't thread safe .
I have been able to fix this issue by replacing the above code with something like:
container.register(SomeType.self) {
SomeType(
dependencyOne: container.synchronize().resolve(Provider<DependencyOne>.self)!
)
}
however that seems wrong, in that it feels like I am creating a cyclic dependency on the container, and also I am not sure that any registrations made after this would be part of the scope of the inlined container, which would result in invalid resolves.
Am I hitting a bug? If so I can try to see if I can fix it, or is there something I am misunderstanding, if so I can try and update the documentation so others won't hit the same issue.