Version 13.0 beta (13A5154h)
swift-driver version: 1.26 Apple Swift version 5.5 (swiftlang-1300.0.19.104 clang-1300.0.18.4)
Target: arm64-apple-macosx11.0
Additional Detail from JIRA
Votes
0
Component/s
Compiler
Labels
Bug
Assignee
None
Priority
Medium
md5: 489030d069b5154dcc245685ee400a89
Issue Description:
Compiler seems to ignore `@MainActor` in the following code example:
importSwiftUIclassModel: ObservableObject {
@Publishedvardata: Data?
}
structTestView: View {
@ObservedObjectvar_model = Model()
@MainActorfuncapply(_data: Data) {
// runtime warning:// Publishing changes from background threads is not allowed; make// sure to publish values from the main thread (via operators like// receive(on:)) on model updates._model.data = data// runtime crashdispatchPrecondition(condition: .onQueue(.main))
}
funcfetchData() asyncthrows {
leturlString = "https://google.com"guardcaseleturl? = URL(string: urlString) else {
return
}
let (data, response) = tryawaitURLSession.shared.data(from: url)
guardcaselethttpResponse? = responseas? HTTPURLResponse,
httpResponse.statusCode == 200else {
return
}
// note there is NO `await` here as the compiler does not ask for it at allapply(data)
}
varbody: someView {
Text("\(_model.data.map(\.count) ?? 0)")
.task {
do {
tryawaitfetchData()
} catch {
print(error)
}
}
}
}
I can try adding `await` infant of the `apply` call, but this results into a `No 'async' operations occur within 'await' expression` warning.
I can apply two workarounds:
Make `apply(_: )` method `async` and add `await` infant of it as the compiler correctly would ask for, but this doesn't make sense as this should be a non-suspendible synchronous method just to hop back to the main thread and sign the value.
I can move `@MainActor` to the entire type and make `fetchData` as `nonisolated`, which works as intended, but the original example should also work and require the `await` keyword.
Environment
Version 13.0 beta (13A5154h)
swift-driver version: 1.26 Apple Swift version 5.5 (swiftlang-1300.0.19.104 clang-1300.0.18.4)
Target: arm64-apple-macosx11.0
Additional Detail from JIRA
md5: 489030d069b5154dcc245685ee400a89
Issue Description:
Compiler seems to ignore `@MainActor` in the following code example:
I can try adding `await` infant of the `apply` call, but this results into a `No 'async' operations occur within 'await' expression` warning.
I can apply two workarounds:
Make `apply(_: )` method `async` and add `await` infant of it as the compiler correctly would ask for, but this doesn't make sense as this should be a non-suspendible synchronous method just to hop back to the main thread and sign the value.
I can move `@MainActor` to the entire type and make `fetchData` as `nonisolated`, which works as intended, but the original example should also work and require the `await` keyword.
Discussion on the forum: https://forums.swift.org/t/mainactor-ignored-by-the-compiler/49533
The text was updated successfully, but these errors were encountered: