Questions tagged [swift-concurrency]

For concurrency language features in Swift, such as async and await. This tag also includes other concurrency features such as actors, Sendable closures, and more.

Swift concurrency with async-await can allow for more readable asynchronous code, an improvement over traditional completion handler closure patterns. “Swift concurrency” also include “actors” (a new object type designed to avoid data races), “tasks” (an object that wraps an asynchronous task, often useful for launching asynchronous action from synchronous context, managing dependencies, or when implementing cancelation support), “sendable” types, etc.

Swift concurrency was introduced in Swift 5.5 in Xcode 13 (supporting macOS 12 and iOS 15), but Xcode 13.3 introduced some backward-compatibility with recent OS versions (e.g., back to iOS 13).

Swift concurrency offers mechanism to retire completion handler closure patterns. In many cases, it obviates the need for code written for and .

References

200 questions
0
votes
2 answers

Add multiple events to the Calendar with Concurrency

I created a function to add my course events to the calendar app using EventKit. After learning the swift concurrency, I want to update my code to make the progress much faster, namely using the detached task or TaskGroup to add these…
0
votes
1 answer

is hashValue of Task unique on Swift concurrency?

I would like to have an array of SomeProtocol which has a get property represents in Task extension. If hashValue of Task is unique, I would like to consider it as a primary key to remove object in SomeProtocol array. And I wonder it is guaranteed…
dev Crying
  • 55
  • 1
  • 7
0
votes
1 answer

How to convert an atypical completion handler to Swift Concurrency

I have in my code some asynchronous method written with a completion handler, and I want to convert it to the new Swift Concurrency syntax. Usually this is simply a matter of wrapping the call in withCheckedThrowingContinuation and calling either…
KPM
  • 10,558
  • 3
  • 45
  • 66
0
votes
1 answer

Synchronous function inside Task or outside - any difference?

Will there be any difference in running a function that is not marked as async and running it inside a Task? .onChange(of: callState.current) { state in viewModel.changeNavigation(title: state.controllerTitle) // 1 - outside Task { …
0
votes
1 answer

SQLite statement Sendable

I'm writing my own SQLite wrapper in Swift and I was wondering if the SQLite statement can be made Sendable or whether it would be better to make it an actor. import SQLite struct Statement: Sendable { private(set) var handle: OpaquePointer? …
Bram
  • 2,718
  • 1
  • 22
  • 43
0
votes
0 answers

Conditionally mark a protocol extension method @Sendable

I have a protocol like this: protocol P { func doThingWithTypeArgument(_ type: T.Type) -> T } extension P { func doThingWithInferredType() -> T { self.doThingWithTypeArgument(T.self) } } When self is sendable, the extension method…
Bbrk24
  • 739
  • 6
  • 22
0
votes
1 answer

Task @Sendable operation

Writing a simple code: class App { private var value = 0 func start() async throws { await withTaskGroup(of: Void.self) { group in for _ in 1...100 { group.addTask(operation: self.increment) // 1 …
97mik
  • 15
  • 2
0
votes
1 answer

How to avoid function being executed in parallel?

I'm using locationManager(_:didVisit:) from Core Location to get notified when the user arrives at or leaves a location. However, I find the callback might be called twice sometimes. Because I would save the data I receive into Core Data, I'd like…
Sheffield
  • 355
  • 4
  • 18
0
votes
1 answer

Does await use main thread or cooperative thread pool?

I know that await does not directly spawn a new thread, but just tell the function to suspend so the system can utilize threads for other work. But when it comes to how / which thread the system will use to continue executing the awaited task / work…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
0
votes
1 answer

Can an actor method be interacted concurrently?

I'm watching this WWDC video about Protect mutable state with Swift actors and in one example they show how an actor method can be called concurrently Imagine we have two different concurrent tasks trying to fetch the same image at the same…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
0
votes
1 answer

Can I call an actor's function from a @Sendable sync function?

I'm not sure if this is even possible, but I thought I should ask anyway! I have a "UserDefaults" client and "live"/"mock" implementations of its interface (taken from a TCA example, but that shouldn't matter). I want to have the mock implementation…
phi
  • 10,634
  • 6
  • 53
  • 88
0
votes
1 answer

How do I write thread-safe code that uses a completionHandler with a function that delegates code to an instance of OperationQueue?

I've been using the CloudKitShare sample code found here as a sample to help me write code for my app. I want to use performWriterBlock and performReaderBlockAndWait as found in BaseLocalCache using a completionHandler without violating the purposes…
daniel
  • 1,446
  • 3
  • 29
  • 65
0
votes
1 answer

How do you execute code when scene goes to background using new Swift async/await concurrency model?

I'm struggling to execute code when app goes to background using new async/await concurrency model in Swift. Here my code: @main struct TestApp: App { @Environment(\.scenePhase) var scenePhase var body: some View { ContentView() } …
Mike
  • 15
  • 1
  • 2
0
votes
1 answer

WKURLSchemeHandler NSInternalInconsistencyException with Task cancellation

I have a problem with WKURLSchemeHandler and Task cancellation and provided an example implementation below. The problem is, that sometimes right after webView(_:stop:) is called (and "Stopping task ..." is printed) either try…
fxvdh
  • 147
  • 9
0
votes
0 answers

How to make python's asyncio.Semaphore in swift?

I'm trying to make something like python's asyncio.Semaphore in Swift. An object like this is used to limit the number of async tasks that that are concurrently accessing a particular resource. Here's something I got that works, but I'm not happy…
Lawrence D'Anna
  • 2,998
  • 2
  • 22
  • 25