Protect mutable state with Swift actors

RSS for tag

Discuss the WWDC21 session Protect mutable state with Swift actors.

View Session

Posts under wwdc21-10133 tag

9 Posts
Sort by:
Post not yet marked as solved
4 Replies
1k Views
When showing an ImageDownloader actor, the presenter said a solution on avoiding duplicate downloads is shown with the code associated with this video. When will this code be available? I don't see it on the video page.
Posted
by
Post not yet marked as solved
1 Replies
1.3k Views
Hi, is there a way that an actor can have a @MainActor @Published annotated property which is then consumed by a SwiftUI View "as usual"? Currently this: @Published @MainActor public private(set) var state: State = .initial gives me the following error when trying to access it from with a SwiftUI View: "Actor-isolated property '$state' can only be referenced from inside the actor" I guess I understand where the error is coming from, but I wonder if there's a way to publish properties from actors and be able to make sure they are updated on the main thread by annotating them with @MainActor.
Posted
by
Post not yet marked as solved
0 Replies
450 Views
I've had an issue in my project where I trigger a task from a SwiftUI View with the modifier .task. From there it calls and async function into an actor that has a TaskGroup that makes concurrent network calls that has a type of Void.self. I've noticed that sometimes the TaskGroup is either never called and will no longer be called. I'm wondering if perhaps a TaskGroup inside of an Actor that has a parent Task created outside the Actor is a problem? But it does seem to work if I change the Actor to a Class. Could someone help explain to me why this is?
Posted
by
Post not yet marked as solved
1 Replies
324 Views
In this talk , at 12:00, the speaker refers to code associated with this talk. But I can't find it anywhere. Is it available?
Posted
by
Post not yet marked as solved
1 Replies
688 Views
Hi, having the concurrency checks (-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks) enabled I always get this warning, when trying to access/use an actor in a SwiftUI .task: "Cannot use parameter 'self' with a non-sendable type 'ContentView' from concurrently-executed code". What would be a correct implementation? Here's a minimal code-sample which produces this warning: import SwiftUI struct ContentView: View { @State var someVar = 5 var a1 = A1() var body: some View { Text("Hello, world!") .padding() .task { await a1.doSomething() } } } public actor A1 { func doSomething() { print("Hello") } }
Posted
by
Post not yet marked as solved
0 Replies
222 Views
I haven't followed the swift forums very closely, so perhaps there is news buried deep somewhere mentioning this. Will the swift compiler and/or TSAN at runtime in the future be able to identify possible race conditions associated with Swift async-await (excluding data races that are "erased" by async-await)? I suppose this could equate to proving a function is reentrant in some scenarios (from a compiler's perspective, though I'm not knowledgeable about compilers)? Consider, e.g. the scenario described in "Protect Mutable State with Swift Actors" around 9:15, where Dario talks about actor reentrancy, with the cache for the image URL
Posted
by
Post not yet marked as solved
0 Replies
252 Views
I'm writing an app that performs actions in a web browser automatically on user's behalf. Currently my app uses OperationQueue to perform asynchronous work. I'm trying to rewrite the app to use async/await instead. Note, that it is necessary for my app to limit the number of concurrently executing work items. Otherwise the app may run into server throttling issues. However, I can't find an equivalent to OperationQueue's maxConcurrentOperationCount parameter in the new concurrency model. Is there a way to limit the number of concurrently running "lanes" of execution without resorting to using OperationQueue / DispatchQueue / NSLock ?
Posted
by
Post not yet marked as solved
4 Replies
189 Views
I made a function that checks over several conditions tu return a string. the function is called when a navigation link is pressed the issue I'm having is that it breaks after the third if statement and only keeps checking after pressing the navigation link for the second time. func diag() {       viewModel.npH = Double(Float(viewModel.pH) ?? 0)       viewModel.npCO2 = Double(Float(viewModel.pCO2) ?? 0)       viewModel.nHCO3 = Double(Float(viewModel.HCO3) ?? 0)       if viewModel.npH > 7.45 {         diagnos.resultado1 = "alcalemia"         print ("1")         }       if viewModel.npCO2 < 40 {         diagnos.resultado2 = "alcalosis respiratoria"         print ("2")         }       if viewModel.nHCO3 > 24 {         diagnos.resultado3 = "alcalosis metabólica"         print ("3")       }               if viewModel.npH < 7.35 {         diagnos.resultado1 = "acidemia"         print ("4")       }       if viewModel.npCO2 > 40 {         diagnos.resultado2 = "acidosis respiratoria"         print ("5")       }       if viewModel.nHCO3 < 24 {         diagnos.resultado3 = "acidosis metabólica"         print ("6")       }       if diagnos.resultado1 == "acidemia" && diagnos.resultado3 == "acidosis metabólica" && (viewModel.expectPCO2aci - 2)...(viewModel.expectPCO2aci + 2) ~= viewModel.npCO2 {         diagnos.resultado4 = "acidosis metabólica compensada"         diagnos.resultado2 = ""         print ("7")         }       if diagnos.resultado1 == "alcalemia" && diagnos.resultado3 == "alcalosis metabólica" && (viewModel.expectALCPCO2 - 0.5)...(viewModel.expectALCPCO2 + 0.5) ~= viewModel.npCO2 {         diagnos.resultado4 = "alcalosis metabólica compensada"         diagnos.resultado3 = ""         print ("8")       }       if diagnos.resultado1 == "alcalemia" && diagnos.resultado2 == "alcalosis respiratoria"         && viewModel.agucro == 1         && (viewModel.expectHCO3alkA - 0.5)...(viewModel.expectHCO3alkA + 0.5) ~= viewModel.nHCO3 {         diagnos.resultado4 = "alcalosis respiratoria compensada"         diagnos.resultado3 = ""         print ("9")       }             }    }
Posted
by