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

GlobalActor directive doesn't guarantee a function will be called on that actor

Assuming I have defined a global actor: @globalActor actor MyActor { static let shared = MyActor() } And I have a class, in which a couple of methods need to act under this: class MyClass { @MyActor func doSomething(undoManager:…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
0
votes
1 answer

Swift concurrency failing on first load

I have a concurrent function running, that is connected to a UIBarbuttonItem .init(barButtonSystemItem: .camera, target: self, action: #selector(runClassification(_:)) Although I have run into a weird issue. Running any task (as simple or complex)…
Harry J
  • 1,842
  • 3
  • 12
  • 28
0
votes
1 answer

How to use Swift async/await for MKLocalPointsInterestRequests

For an app for visual impaired persons, I want to show POI's in a view. I use code below. Although the lowest level (requestNearbyLocations) prints info to the debug screen, I'm not able to return this data (locdata) to the calling method, so nor to…
WimJanse
  • 13
  • 2
0
votes
0 answers

ThreadSanitizer data race warning in `Task`

I am running Unit tests with Thread Sanitizer enabled and I am getting a Data Race warning when executing the statement try? await Task.sleep(nanoseconds: 10_000_000) Update I was able to reproduce the data race warning with only Swift Combine and…
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
0
votes
1 answer

SwiftUI: NavigationLink doesn't show animation if setting after background task

I am trying to use the new async/await concurrency code with a SwiftUI view to do some basic asynchronous data loading after a button click, to show a spinner view, and on completion, transition to a new page. I use an enum to mark the state of the…
Z S
  • 7,039
  • 12
  • 53
  • 105
0
votes
2 answers

How to access an actor from within a filter closure

I'm having trouble working out how to access an actor from a filter closure. Take this example code: import Foundation actor MyActor { var contents: [String] init() { self.contents = [] } } let myActor = MyActor() let…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
0
votes
0 answers

SwiftUI: asynchronously making a calculating and show its result in a sheet

I'm making a CrossFit app that has, as one of the features, a stopwatch or timer that the user will start and stop, and when it's stopped, it'll log the exercise to Health Kit, it will calculate the approximate calories based on the exercise…
noloman
  • 11,411
  • 20
  • 82
  • 129
0
votes
1 answer

Xcode 13.3 new error message with @MainActor

I updated today to Xcode 13.3 and now my code can't compile anymore. I get two error messages. It seems there is a connection between the errors. First error: Property 'startAnimation' isolated to global actor 'MainActor' can not be mutated from a…
Michael
  • 616
  • 5
  • 20
0
votes
1 answer

Xcode giving warning when using await UIApplication.shared.open(settingsURL) in Swift async function

I am calling await UIApplication.shared.open(settingsURL) inside a Swift Task but I am getting an Xcode runtime warning: UIApplication.open(_:options:completionHandler:) must be used from the main thread only Task { guard let settingsURL =…
shim
  • 9,289
  • 12
  • 69
  • 108
0
votes
0 answers

Using `async` method synchronously

I am moving some of my asynchronous functions to the new Swift Concurrency mechanism. At some point, I transform a method with a completion handler to a synchronous method using DispatchGroup (it can looks weird but this is because I use this method…
Florentin
  • 1,433
  • 2
  • 13
  • 22
0
votes
1 answer

Most efficient way to iterate over async method to produce an array

If you have an async method that produces a value, what is the most efficient way to iterate over that method to produce an array of values? protocol ImageFetching { var title: String { get } func fetch(from url: URL) async throws ->…
Rob C
  • 4,877
  • 1
  • 11
  • 24
0
votes
3 answers

How do I serialize access to all Void returning functions of a class (avoiding race conditions)?

Suppose a class that's arranged something like this: class A { func a() { doStuff() } func b() { Task { something() await someLongRunningThing() somethingElse() } } func…
Steve M
  • 9,296
  • 11
  • 49
  • 98
0
votes
2 answers

How to wait until data from network call comes and only then return value of a function #Swift

I have a service class that makes an api call and stores data into its property. Then my interactor class have a method where I want to make service class api call and when data will be stored - return it. I tried myself to handle this with…
0
votes
0 answers

Invoke concurrent functions sequentially

Overview I have a function f1 which is non-async function. f1 gets called multiple times and I have no control over the calling of f1 When f1 gets called I would like to invoke an async function f2 Aim: I would like f2 to complete before the next…
user1046037
  • 16,755
  • 12
  • 92
  • 138
0
votes
1 answer

Actor ‘self’ can only be passed ‘inout’ from an async initializer

In Swift, let’s say we have an actor, which has a private struct. We want this actor to be reactive, and to give access to a publisher that publishes a specific field of the private struct. The following code seems to work. But it produces a warning…
Frizlab
  • 846
  • 9
  • 30
1 2 3
13
14