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

Why can't I catch a timeout error in this async function that detects if a device is paired with a watch?

I am attempting to write a utility function so that I can know if an iOS device is paired with an Apple Watch with something as simple as: func isPaired( timeoutAfter maxDuration: TimeInterval ) async throws -> Bool { I came along…
Mick F
  • 7,312
  • 6
  • 51
  • 98
0
votes
1 answer

Access global actor property from its shared instance

I'm learning about the new concurrency model that Swift offers with its actor mechanism and I'm having a hard time figuring out the concrete use of a globalActor (except from @MainActor). Given this example: @globalActor actor TestGlobalActor { …
Olympiloutre
  • 2,268
  • 3
  • 28
  • 38
0
votes
1 answer

Convert the async Throwing Expression to a Result

According to Preserving the Results of a Throwing Expression documentation it is possible to convert throwing expression into Result type as follows: let singleSample = Result { try UnreliableRandomGenerator().random() } Is it also possible to…
softshipper
  • 32,463
  • 51
  • 192
  • 400
0
votes
1 answer

Async function with thread safety

Consider the following function snippet: public func update(tables: [LocalTableItem]) async throws -> UpdateResult { let result = try await store.remove(tables: tables) print("** removing table \(tables.last!.name)") …
0
votes
1 answer

Cancellation not propagated

Cancellation is not propagated through the service class here let task = Task { if Task.isCancelled { throw URLError(.cancelled) } return try await service.loadResource() } What modification is required…
bobby123uk
  • 892
  • 4
  • 17
0
votes
1 answer

Thread safe combine publisher to AsyncStream

I try to figure out is this approach thread safe if getStream() and update(value: ...) will be called on difference thread simultaneously? final class SomeNotifier { static let shared = SomeNotifier() private let value = PassthroughSubject
FuzzzzyBoy
  • 148
  • 1
  • 13
0
votes
0 answers

RevenueCat SDK 4.17.8: Correct way to @Publish up-to-date subscription status in SwiftUI using Swift concurrency

I have an existing RevenueCat implementation in SwiftUI using the Purchases SDK 3.14.1 in production. I am currently in the process of upgrading to the latest RevenueCat SDK 4.17.8 and am therefore reconsidering my design choices and looking for…
rdor
  • 101
  • 1
  • 4
0
votes
1 answer

upload frames from ARSession to firebase storage in real time

I'm trying to stream frames from ARSession to Firebase Storage in real-time. There is some preprocessing that is done on frames before they are sent to firebase. The flow of app is as follows. The frameCount variable is used used to name the image…
0
votes
1 answer

Observer pattern with actor

I try to implement some repository that can notify about change its state. protocol Observer { } actor Repository: Actor { var observers: [Observer] = [] func add(observer: Observer) { self.observers.append(observer) } } and…
FuzzzzyBoy
  • 148
  • 1
  • 13
0
votes
0 answers

Giving a time limit for an async function to execute in swift

I am trying to give a time limit of 2 seconds and total trials of 3 for an async network call to execute and finish. and after the 2 seconds, the call should try one more time then exists with a failure. I tried this. but its not exiting with an…
AbEls
  • 1
0
votes
0 answers

Assign Thread to Swift Concurrency Actor

I'm processing ~10k events between two different classes. One is fetching them, and the other is storing them in a dictionary. Now since the fetching class is also doing more stuff with the data than just passing it to the second class, it really…
yspreen
  • 1,759
  • 2
  • 20
  • 44
0
votes
1 answer

XCTest async code doesn't get executed in 'tearDown() async throws' method

From Apple's XCTest doc regarding setUp and tearDown it said to perform async code in tearDown() async throws method. And in my case, I create some test data in server with API call, and I'd like to clean it after the test case execution is…
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
0
votes
1 answer

How I can solve issue with assigning view function to StateObject propety

I am trying to assign regular function like this var body: some View { contentView .task { navigation.tabBarVisibility = .hidden viewModel.fetchWordset() …
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
0
votes
4 answers

Can we define an async/await function that returns one value instantly, as well as another value asynchronously (as you'd normally expect)?

Picture an image loading function with a closure completion. Let's say it returns a token ID that you can use to cancel the asynchronous operation if needed. @discardableResult func loadImage(url: URL, completion: @escaping (Result)…
Matjan
  • 3,591
  • 1
  • 33
  • 31
0
votes
2 answers

How to adapt RunLoop to swift concurrency(async/await)

I'm having some compilation warnings using Xcode 14.2/swift 5.7 (future errors in swift 6). I've some async function tests in a unit test target which include some code to process UI changes in the main loop. There are two related…
kikeenrique
  • 2,589
  • 2
  • 25
  • 46