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
4
votes
1 answer

How to return variable defined in Task in Swift

I have a class Artist, and below is a function to create a new artist and insert it to my library array. Because insertToArtists(artist:) is a asynchronous function, I have to wrap it in Task. But I also want my newArtist to return the artist just…
Yiming Designer
  • 423
  • 3
  • 10
4
votes
1 answer

How to support an async callback with timeout in swift actor

I have a mix of async/await (Actors) running "legacy" code (Process Pipes and callback blocks). I have a situation similar to this where my actor's function returns a value, while also storing a deferred callback once some work is completed ( actor…
Avba
  • 14,822
  • 20
  • 92
  • 192
4
votes
1 answer

Using @MainActor in a Task which is calling async function declared as @MainActor

I would like to understand if it's needed to declare the Task itself as MainActor considering the following block of code. func login() { Task { [weak self] in let result = await self?.loginService.start() if result ==…
Zagahr
  • 43
  • 4
4
votes
2 answers

Type erasure in Swift Concurrency AsyncStream

Experimenting with Swift's concurrency, I would like to have a clean API for exposing an async sequence of a given element type and a throttled version of the same: var intStream: AsyncStream { AsyncStream(Int.self, bufferingPolicy:…
Nicolas Degen
  • 1,522
  • 2
  • 15
  • 24
4
votes
1 answer

Accessing actor properties synchronously from task-less context

Integrating actors with existing code doesn't really seem to be as simple as Apple wants you to believe. Consider the following simple actor: actor Foo { var value: Int = 0 } Trying to access this property from any AppKit/UIKit (task-less)…
user187676
3
votes
2 answers

Swift concurrency: Why is this method async?

I'm trying to wrap my head around how to integrate Swift concurrency with old code that is using block-based things like Timer. So when I build the code below, the compiler tells me on the line self.handleTimer() that the Expression is 'async' but…
Dorian Roy
  • 3,094
  • 2
  • 31
  • 51
3
votes
0 answers

Reading an AsyncSequence up to a delimiter

I'm reading and writing to an external process using two Pipes. Every command response will be terminated by "\n] ". I'm currently reading from the pipe as an AsyncSequence (readPipe.fileHandleForReading.bytes.characters). I'd like to read from the…
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
3
votes
1 answer

Difference between starting a detached task and calling a nonisolated func in main actor

Imagine I'm in a class annotated with @MainActor so that all the functions are tied to the main actor. I'm trying to understand what the difference is between doing the following: func bar() { Task.detached(priority: .background) { await…
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
3
votes
2 answers

Split up Task into multiple concurrent subtasks

I am trying to do some calculations on a large number of objects. The objects are saved in an array and the results of the operation should be saved in a new array. To speed up the processing, I‘m trying to break up the task into multiple subtasks…
Gary
  • 1,020
  • 1
  • 8
  • 20
3
votes
1 answer

What is the Swift concurrency equivalent to a promise–resolver pair?

With the PromiseKit library, it’s possible to create a promise and a resolver function together and store them on an instance of a class: class ExampleClass { // Promise and resolver for the top news headline, as obtained from // some web…
bdesham
  • 15,430
  • 13
  • 79
  • 123
3
votes
2 answers

What's the relationship between 'Task' and 'Actor' in Swift Concurrency

As we all know: Task.init creates an unstructured task that runs on the current actor, Task.detachedcreates an unstructured task that’s not part of the current actor. I understand those differences when the context of creating a new unstructured…
Li Fumin
  • 1,383
  • 2
  • 15
  • 31
3
votes
2 answers

Unit testing a class that depends on an async function

I have a view model with a state property enum that has 3 cases. protocol ServiceType { func doSomething() async } @MainActor final class ViewModel { enum State { case notLoaded case loading case loaded } …
Darren Findlay
  • 2,533
  • 2
  • 29
  • 46
3
votes
1 answer

NS_SWIFT_UI_ACTOR annotation not working with async variants of callback based objective-C methods

In Objective-c we can use NS_SWIFT_UI_ACTOR to indicate that a method or a whole class should be executed on the main actor (as we would do with @MainActor in Swift). This works correctly since, when invoking a method which returns a value from…
francybiga
  • 960
  • 1
  • 8
  • 16
3
votes
1 answer

Backporting URLSession's download(for:delegate:) for concurrent usage

I was trying to backport URLSession's download(for:delegate:) since it requires a deployment target of iOS 15+ but concurrency is now supported for iOS 13+. It seemed like a very straight forward process since you can pass a continuation into the…
ph1psG
  • 568
  • 5
  • 24
3
votes
1 answer

SWIFTUI: Invalid conversion from 'async' function of type '(IndexSet) async

I am learning SWIFTUI and trying to do some app to test my knowledge without actually following a tutorial, as I like to wrap my head around why things work certain way. It's how I learn :) So, I am trying to call a function to post some data…
SwiftExpress
  • 37
  • 1
  • 5