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

await withTaskGroup with Core Data sometimes fails

I have the following taskGroup, using the async/await concurrency in Swift 5.5, where I iterate over meters, fetching onboardingMeter from Core Data. This works ok in around 4 out of 5 times, but then crashes the iOS app. I am testing this with…
Ivan C Myrvold
  • 680
  • 7
  • 23
1
vote
1 answer

Swift async let cancellation doesn't work

I am a bit confused about tasks being cancelled. Overview: checkCancellation function has 2 child tasks, one task runs computeA and the other computeB. They run concurrently, computeB throws an error. Doubt: I expected child task computeA to be…
user1046037
  • 16,755
  • 12
  • 92
  • 138
1
vote
1 answer

Updating a @State property from within a SwiftUI View

I have an AsyncContentView that handles the loading of data when the view appears and handles the switching of a loading view and the content (Taken from here swiftbysundell): struct AsyncContentView, Content: View>:…
Darren
  • 10,182
  • 20
  • 95
  • 162
1
vote
1 answer

SwiftUI macOS document app architecture in a concurrent world

I'm trying to figure out the correct structure for a macOS document app using SwiftUI and Swift 5.5 concurrency features. I want to demonstrate updating a document's data asynchronously, in a thread safe manner, with the ability to read / write the…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
0
votes
2 answers

How can I update a @Published value from a Timer block without warnings

I have the following code: class Recorder: ObservableObject { @Published var recordingTimeSeconds: TimeInterval = 0 private var timer: Timer? init() {} func startRecording() { timer = Timer(timeInterval: 1, repeats:…
MMise
  • 69
  • 6
0
votes
2 answers

Why and how do 'Data(contentsOf: URL) call' and 'URLSession.shared.data(from: URL) call' each as a child task to a TaskGroup make a difference?

I am trying to build a chat app. I request the messages through API. Since the chat might be an image(a URL for that image in this case), and every image has different height / width ratio, I’m trying to fetch their ratios before reloading the table…
0
votes
0 answers

withTaskGroup runs slower on multiple cores

strange phenomenon. I'm doing some particle simulation, doing for n particles n*n computations: private func computeVelocitiesSingle() async { // over all particles (0 ..< self.particles.count).forEach { i in let p =…
osx
  • 146
  • 1
  • 11
0
votes
0 answers

How to detect macOS Shortcuts cancellation in an AppIntent perform()?

I'm testing with this simple AppIntent that only counts from 1 to 10 each second. import AppIntents struct Loop10SecCount: AppIntent { static var title: LocalizedStringResource = "Loop 10 second count" static var description =…
Hlung
  • 13,850
  • 6
  • 71
  • 90
0
votes
1 answer

Current count value of DispatchSemaphore

DispatchSemaphore used to restrict the concurrent access on shared resources. It pauses the thread using counter value. Is it possible to get that counter value?
iroh
  • 23
  • 5
0
votes
0 answers

Learning SwiftUI view does not update from time to time

I think I'm doing something wrong here. Problem is that when this example app is running and when I'm trying to Reload it can stuck randomly on loading screen and thats it. View didn't respond to any update of state again. It just displays…
Marcin Kapusta
  • 5,076
  • 3
  • 38
  • 55
0
votes
1 answer

Swift Unit Testing Network Request

I want to write unit tests for my DataService. I read that you don't actually want to make network requests in the tests, and instead, you should make a MockDataService. However, what should the body of the fetchBusinesses method look like in the…
aDabOfRanch
  • 137
  • 9
0
votes
2 answers

async/await: How do I run an async function within a @MainActor class on a background thread?

Let’s say I have an async function within a @MainActor class that performs some slow, synchronous, CPU-intensive operation: @MainActor class MyClass { // ... func getFoo() async -> Foo { let foo = // ... some slow, synchronous,…
NSExceptional
  • 1,368
  • 15
  • 12
0
votes
1 answer

Swift concurrency - having functions run on specific threads

I have two questions to clarify what I understood is correct: Regarding async functions: If we wanted to run the same async function sometimes on the main thread and at other times on other threads, you cannot do so like you would with…
funct7
  • 3,407
  • 2
  • 27
  • 33
0
votes
0 answers

Make Task await until other task is complete

I have an async function that can be started from two location. Please note that within the getRecentMovies, there is a Task {} that makes an async network call. The first way to trigger the function is from: .onChange(of: scenePhase) { newPhase in …
mikemgg123
  • 69
  • 4
0
votes
1 answer

SWIFT TASK CONTINUATION MISUSE: leaked its continuation and unable to return result after for loop

I receive the following warning: SWIFT TASK CONTINUATION MISUSE: saveAndClose() leaked its continuation! Inside for loop, after executing few items, at one of the item, in save and close function, it is blocked, and says above error, and not…