-
Hello! I have started a new app and I'm facing some issues with Dependencies and a dependency marked with MainActor. I have a CoreData client marked as MainActor and I'm trying to use it in a dependency like this: protocol ObserveDuplicateGroupsUseCase {
func execute() async -> AsyncThrowingStream<[[String]], Error>
}
@MainActor
struct ObserveDuplicateGroupsUseCaseLive: ObserveDuplicateGroupsUseCase, Sendable {
let client: CoreDataClient
func execute() async -> AsyncThrowingStream<[[String]], Error> {
// Code fetching data here
}
}
private enum ObserveDuplicateGroupsUseCaseKey: DependencyKey {
@MainActor
static let liveValue: any ObserveDuplicateGroupsUseCase = ObserveDuplicateGroupsUseCaseLive(
client: CoreDataClient.buildDefault()
)
} This logs a warning "Main actor-isolated static property 'liveValue' cannot be used to satisfy nonisolated protocol requirement; this is an error in the Swift 6 language mode" and removing the MainActor from liveValue transform warning into this error: "Call to main actor-isolated static method 'buildDefault()' in a synchronous nonisolated context". What's the best way to handle a case like this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @bitomule, currently // Not @MainActor
class Model {
@Dependency(\.mainActorClient) var …
} This would require initializing a Theoretically the tools in the library could start holding onto their isolation as However, typically one does not need to make the dependency itself |
Beta Was this translation helpful? Give feedback.
Hi @bitomule, currently
@Dependency
can be used from basically anywhere, and that meansliveValue
(andtestValue
/previewValue
) must be non-isolated. You cannot have a dependency that is@MainActor
bound because nothing is stopping you from doing:This would require initializing a
@MainActor
thing from a non-necessarily@MainActor
context.Theoretically the tools in the library could start holding onto their isolation as
(any Actor)?
, but it's a big unknown of what that would do to ergonomics, and I don't think Swift's isolation tools are even up for the challenge. You would have static information of the type of iso…