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

Using Factory in a Sendable conforming Class #168

Closed
sebanitu opened this issue Nov 17, 2023 · 1 comment
Closed

Using Factory in a Sendable conforming Class #168

sebanitu opened this issue Nov 17, 2023 · 1 comment

Comments

@sebanitu
Copy link

Hi! First of wall a huge thank you for all of the hard work you've put into Factory, I love it!
I was wondering if there is any way in which @injected properties can be used within a Sendable conforming Class while also avoiding the Concurrency warning related to mutability.

Example:

final class Service: Sendable {
    @Injected(\.apiClient) private var apiClient
    ....
}

-> throws a warning "stored property '_apiClient' of 'Sendable'-conforming class 'Service' is mutable"

Thanks you in advance for looking at this!

@hmlongco
Copy link
Owner

There doesn't appear to be an easy way to fix @injected at the moment.

As such, your two options appear to be..

  1. Marking the class as @mainactor. Which may or may not cause MainActor cascades.
@MainActor
final class Service: Sendable {
    @Injected(\.apiClient) private var apiClient
    ....
}
  1. Wrapping the value in a lock.
let apiClient = OSAllocatedUnfairLock(uncheckedState: Container.shared.apiClient)

May have to look into an @InjectSendable wrapper....

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

2 participants