-
|
Excuse my stupid question but what could be the issue with this code, which crashes due to uninitialized parameters. Removing the @DependencyClient macro resolves the issue but I'd like to keep it for the argument labels. @DependencyClient
public struct MyDependency {
public var test: (_ one: String, _ two: String) async throws -> Void
}
extension MyDependency: DependencyKey {
public static var liveValue: Self {
return Self(
test: { one, two in
print("Calling test with \(one) and \(two)")
}
)
}
}
extension DependencyValues {
var myDependency: MyDependency {
get { self[MyDependency.self] }
set { self[MyDependency.self] = newValue }
}
}
@Reducer
struct Content {
@ObservableState
struct State: Equatable {
}
enum Action {
case tapped
}
@Dependency(\.myDependency) var myDependency
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .tapped:
return .run { send in
try await myDependency.test("one", "two")
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hi @bugix, this code does not crash for me. Are you sure it actually crashes? I do see a test failure:
But that is to be expected. By default accessing a dependency in a test is a test failure because typically you should be forced to override that dependency so that you don't accidentally access a live dependency in tests. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @mbrandonw , thanks for the quick answer. Yes, I think it crashes or at least throws a "EXC_BAD_ACCESS" error.
I did not create a test target, so I don't belive the missing test value is the issue. The project was created by Xcode 26.1.1 with the default (concurrency-)settings. Maybe I miss some needed adjustments there? I uploaded the project to https://github.com/bugix/DependencyPoc |
Beta Was this translation helpful? Give feedback.

Hi @bugix, thanks for providing the project, now I see what you are talking about.
Sadly this is just a sharp edge of using Swift 5 language mode with modern concurrency tools and minimal concurrency checking. It leads to compiling code that can crash at runtime. The crux of the problem is that you are using default main actor isolation, so everything is implicitly main actor, but key paths on main actor types are not
Sendable, and that can lead to accessing main actor data on non-main threads. And because you are using Swift 5 mode and "minimal" concurrency checking (the default in Xcode), there is no warning or error about this problem.Due to the fact that Swift has lots of "modes" and…