Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weak object scope acts like a singleton #492

Open
jakubkiermasz-zd opened this issue Aug 17, 2021 · 0 comments
Open

Weak object scope acts like a singleton #492

jakubkiermasz-zd opened this issue Aug 17, 2021 · 0 comments

Comments

@jakubkiermasz-zd
Copy link

jakubkiermasz-zd commented Aug 17, 2021

Hi!

I'm playing with the swinject and its weak object scope. It works like a charm, when I'm resolving an object without any arguments. When I want to register and resolve an object with one/more arguments, it acts as a singleton.

Here's the class declaration and registration

container.register(WeakFakeWithDependencyClass.self) { (_, argument: DependencyClass) -> WeakFakeWithDependencyClass in
    WeakFakeWithDependencyClass(dependency: argument)
}
.inObjectScope(.weak)

fileprivate final class WeakFakeWithDependencyClass {
    let dependency: DependencyClass
    init(dependency: DependencyClass) {
        self.dependency = dependency
    }
}
fileprivate final class DependencyClass {}

And here's the test result:

var firstAccessInstance: WeakFakeWithDependencyClass? = .none
firstAccessInstance = sut.resolve(WeakFakeWithDependencyClass.self, with: DependencyClass())
let firstAccessObjectIdentifier = ObjectIdentifier(firstAccessInstance!)
firstAccessInstance = .none
let secondAccessObjectIdentifier = ObjectIdentifier(sut.resolve(WeakFakeWithDependencyClass.self, with: DependencyClass()))
XCTAssertNotEqual(firstAccessObjectIdentifier, secondAccessObjectIdentifier)

Witch fails with an XCTAssertNotEqual failed: ("ObjectIdentifier(0x00007f9e6470f1d0)") is equal to ("ObjectIdentifier(0x00007f9e6470f1d0)") error. What can cause such issue?

Btw I'm using Xcode 12.5, Swift 5.4, Swinject 2.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant