12:04
Thank you so much for detailed examples @Rob. What I meant to say with my message "I can see that execution is not concurrent but parallel" was instead "I can see that execution is not concurrent but serial"
``` let poi = OSLog(subsystem: "Test", category: .pointsOfInterest) class DummyVC: ObservableObject {} struct ContentView: View { @ObservedObject var dummy = DummyVC() var body: some View { VStack { Button("Launch Ten Tasks") { launchTenTasks() } } .padding() } func launchTenTasks() { let experiment = Experiment() for i in 0 ..< 10 { Task { experiment.spin(index: i, for: 1)
12:30
Oh well, I think I managed to answer my question. The fact that we have @ObservedObject in a view means that the view will be implicitly isolated to the MainActor due to "A struct or class containing a wrapped instance property with a global actor-qualified wrappedValue infers actor isolation from that property wrapper:
" from: https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md
" from: https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md
7 hours later…
19:47
@Petar – Agreed. Adding the
@ObservedObject
property to the View
ends up actor-isolating the whole ContentView
, and thus launchTenTasks
is suddenly isolated to the main actor. And now that launchTenTasks
is actor-isolated, then Task {…}
will create top-level tasks on behalf of the main actor, which will prevent parallel execution.
The moral of the story is that if you want parallel execution, it is not prudent to use
Task {…}
and rely upon the fact that you just happened to be calling it from a function that is in a type that is not actor isolated. Either explicitly make launchTenTasks
a nonisolated
function, or use Task.detached {…}
, or a task group, or async let
. « first day (6 days earlier) ← previous day last day (15 days later) »
Transcript for
Dec13
Dec '2318
Jan2
Are Swift Concurrency tasks run in se…
Comments arising from stackoverflow.com/q/74330956/1271826