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

Actor-isolated property cannot be passed 'inout' to 'async' function call

I'm new (like most everyone, I suppose) to Swift concurrency, and I'm running into a compiler error I don't know what to do with. struct Thing { var counter = 0 mutating func increment() async { counter += 1 } } class Controller:…
Sam
  • 1,504
  • 2
  • 13
  • 19
3
votes
2 answers

How to await an arbitrary number of async functions in parallel

The Swift Concurrency documentation provides the following example for how to await multiple async functions in parallel. async let firstPhoto = downloadPhoto(named: photoNames[0]) async let secondPhoto = downloadPhoto(named: photoNames[1]) async…
pdiffley
  • 603
  • 9
  • 18
3
votes
0 answers

Swift Concurrency: Conforming a 3rd party library to actor isolation

I am in the process of converting some code bases over to using Swift concurrency and am running into a few snags along the way. The current project I'm working through has a few 3rd party libraries that it relies on, and in one of those libraries,…
RPK
  • 1,830
  • 14
  • 30
3
votes
1 answer

Using async/await (Swift 5.5) with firebase Realtime Databse

I use firebase's Realtime Database to make asynchronous database queries from my app. Now that iOS 15 gives us Swift 5.5, I'd love to use async/await to perform those queries instead of passing completion closures. I know that I can use await…
3
votes
1 answer

Error: 'async' call cannot occur in a property initializer

I'm trying to place a StateObject actor whose initializer is async in an App, but I can't find a way to do that. Let's say I have this actor: actor Foo: ObservableObject { init() async { // ... } } This results in the titular…
Ky -
  • 30,724
  • 51
  • 192
  • 308
3
votes
1 answer

Conforming an actor to Sequence protocol

How can one conform an actor to the Sequence protocol? The following code generates the compiler warning: Instance method 'makeIterator()' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
3
votes
2 answers

What is the correct way to await the completion of two Tasks in Swift 5.5 in a function that does not support concurrency?

I have an app that does some processing given a string, this is done in 2 Tasks. During this time i'm displaying an animation. When these Tasks complete i need to hide the animation. The below code works, but is not very nice to look at. I believe…
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
2
votes
1 answer

Swift Concurrency - Sync and Async Task

Using GCD, I am able to run a synchronous task after an asynchronous task has finished. let queue = DispatchQueue(label: "for.test") var exampleList = [String]() queue.async { exampleList.append("Hello") } queue.sync { for item in exampleList…
2
votes
1 answer

Type erasing `AsyncMapSequence, Element>`

I'd like to type erase AsyncMapSequence, Element>. My consumer doesn't care whether the sequence has been transformed, or how it has been transformed. Ideally this could be type erased to the original stream type…
Will Alexander
  • 337
  • 2
  • 16
2
votes
2 answers

How to resolve memory leak with Combine's AsyncPublisher

Having memory leak issue while asynchronously iterating over AsyncPublisher (kind of async sequence)! In the following code I have timerSequence (AsyncPublisher>) and on inti I'm asynchronously iterating…
SPatel
  • 4,768
  • 4
  • 32
  • 51
2
votes
1 answer

PassthroughSubject's AsyncPublisher values property not producing all values

I created a small wrapper around a PassthroughSubject which converts it into an AsyncStream. protocol Channelable {} final class Channel { private let subject = PassthroughSubject() func send(_ value: Channelable)…
hydro1337x
  • 95
  • 5
2
votes
2 answers

when use nonisolated with stored properties of actor?

I am not sure when use nonisolated with stored properties of actor. P.s In this blog Douglas Gregor asking same question "So... just always require nonisolated let for synchronous access?". But I don't understand his answer. Who can explain please…
2
votes
1 answer

Why does a @MainActor annotated class initialisation gives the error

I have a class named Person, Which is annotated with @MainActor. But as I try to create an instance of this mainactor class it gives an error. "Call to main actor-isolated initializer 'init(firstName:lastName:)' in a synchronous nonisolated…
the monk
  • 389
  • 4
  • 14
2
votes
1 answer

Difference of TaskPriority for Task.cancel()

I tried this post https://www.swiftbysundell.com/articles/building-an-async-swiftui-button/ where the code is from but have added the priority constant in the struct. The Button is doing what it should be (only show the ProgressView when the sleep…
sheldor
  • 115
  • 12
2
votes
0 answers

Swift actor: unrecognized selector sent to instance 0x8000000000000000

I am trying to perform an API call on a list of Draft which are queued< (1 call by Draft). For this I have created a DraftQueue: actor DraftQueue { private var taskQueue = [String: Task]() func cleanQueueElement(uuid: String)…
Ambrdctr
  • 21
  • 2