Ali Baqbani
Ali Baqbani

Reputation: 153

Swinject factory closure called multiple times when object scope is container

I'm trying to register CoreDataStack as a single instance on a shared container with object scope .container, but its factory closure called multiple times and when I debug memory graph in Xcode several instances of the object are created even when the returned object is not value-type!

Swinject documentation:

The object scope is ignored if the factory closure returns a value type because its instance is never shared per the Swift specification.

What is the root cause of this strange behavior?

Upvotes: -2

Views: 550

Answers (1)

Ali Baqbani
Ali Baqbani

Reputation: 153

Registering a service with a name solve the issue and no more instances created

container.register(CoreDataStack.self, name: "CoreDataStack") {
        DefaultCoreDataStack(modelName: "name")
 }.inObjectScope(.container)

Upvotes: 1

Related Questions