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
2 answers

How can I modify the code below so the order of Image selection matches the order of Image presentation using swift concurrency?

This is how the images were selected. This is how they are presented. import SwiftUI import PhotosUI @MainActor class ExampleViewViewModel: ObservableObject { @Published var selectedItems: [PhotosPickerItem] = [] @Published var…
byaruhaf
  • 4,128
  • 2
  • 32
  • 50
1
vote
2 answers

Tasks always seem to run in order in Swift code

This code always seems to work like magic. Without any locking, the output is 1,2. class Counter { var count = 0 func increment() -> Int { count += 1 return count } } class ViewController: UIViewController { var…
WishIHadThreeGuns
  • 1,225
  • 3
  • 17
  • 37
1
vote
1 answer

Is it safe to call DispatchQueue sync for read operations?

I have a class that reads from and writes to a dictionary using a serial dispatch queue. While the write operations are asynchronous, the read operations are synchronous. I want to avoid the use of a completion handler for reading. Does this create…
DesperateLearner
  • 1,115
  • 3
  • 19
  • 45
1
vote
2 answers

How to ignore async let throws error when save response in tuple?

i have a code like this: Task { async let configsReq = self.interactor.getConfigs() async let optionsReq = self.interactor.getOptions() async let updateStateReq = self.interactor.getAppUpdateState() async let contactsReq =…
Sajjad
  • 1,536
  • 2
  • 17
  • 31
1
vote
0 answers

iOS structured concurrency - How can i use Sendable to stop strict concurrency warnings

In the class below, I get the following warning Capture of 'self' with non-sendable type 'TestClass' in a '@Sendable' closure" I realise i could conform TestClass to Sendable in this example, but in my actual app that feels problematic as it would…
1
vote
2 answers

Data race occurring in Swift Actor

I found a data race in my Swift app by using the Thread Sanitizer, and so I made my first attempt to fix race conditions by converting the offending class to an actor. The crash that the race was causing seems to have gone away, but Thread Sanitizer…
RL2000
  • 913
  • 10
  • 20
1
vote
2 answers

Can a class be Sendable if it holds a reference to a non-Sendable singleton?

Suppose I have a class whose only state is a reference to a singleton class: class MyClass { let networkHelper: NetworkHelper func makeNetworkRequest(_ url: URL) { networkHelper.actuallyMakeNetworkRequest(url) } } The…
bdesham
  • 15,430
  • 13
  • 79
  • 123
1
vote
1 answer

How to call async methods in an Array map or forEach?

I'm making my first attempts with Swift's async/await features and Task. My first goal is working with NSItemProvider. I have a custom class that extends NSObject and conforms to NSSecureCoding. I have working code that converts my class to and from…
HangarRash
  • 7,314
  • 5
  • 5
  • 32
1
vote
1 answer

Calling UIViewController dismiss and/or present in an async context?

According to Calling Objective-C APIs Asynchronously methods imported from Objective-C that meet certain requirements are "imported as two methods", where one is async instead of having a completion block. If the method has more than one parameter,…
shim
  • 9,289
  • 12
  • 69
  • 108
1
vote
0 answers

Using withTaskCancellationHandler in MainActor-isolated context raises warnings

In swift-concurrency, we will get a warning from Xcode when we use withTaskCancellationHandler in main-actor-isolated context as the attached screenshot. How we can solve this warning? in case of using in main-actor context. I think I can…
Muukii
  • 91
  • 6
1
vote
0 answers

Running @MainActor in non isolated context with Thread.isMainThread check

I have a non-isolated property. Inside this I need to check whether it's being called from the main thread. If it is, then I want to immediately call a main actor isolated function. I have tried the following: var myProperty: Bool { if…
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
1
vote
1 answer

Shorthand for calling Concurrency within Combine sink?

I'm subscribing to Combine publishers, but frequently calling Concurrent tasks within the .sink. Is there a more convenient way to do this? import _Concurrency import Combine import Foundation import PlaygroundSupport var cancellable =…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
2 answers

How to Display the async/throw functions return value inside the textView?

This is the function I am calling, which uses the latitude and longitude, converts them into city name and country name, and then returns city name or country name depending on what I want. import SwiftUI import CoreLocation struct…
1
vote
1 answer

How to resolve - compiler warning that UndoManager is non-sendable

I have code where a set of functions need to run under a global actor context, but have undo capability. When I call them I get the following compiler warning (Xcode 14). Non-sendable type 'UndoManager?' passed in call to global actor…
Philip Pegden
  • 1,732
  • 1
  • 14
  • 33
1
vote
2 answers

How to wait for a bunch of async calls to finish to return a result?

I understand the basic usage of async/await but I'm a bit confused of what I should do in this specific example. I have an async function called save(url: URL) which mimics a function that would take a local URL as its parameter, and asynchronously…
Dannis Case
  • 607
  • 2
  • 5
  • 10