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

How to get concurrency when using AsyncLines

I'm trying to use AsyncLineSequence with Process to execute many instances of a shell script at the same time. The issue I'm seeing is that with my usage of AsyncLineSequence I'm not seeing the output of the Process invocations interweaved like I…
Paul.s
  • 38,494
  • 5
  • 70
  • 88
1
vote
1 answer

How to pause an asynchronous Swift function until a callback is called when a result is available from another library(JavaScriptCore)

I want to use Vapor(a Web framework made with Swift) and JavaScriptCore(Apple's JS engine) to process some requests. Vapor supports async/await, which makes handling HTTP requests very easy, like this: func routes(_ app: Application) throws { …
mrtksn
  • 412
  • 3
  • 18
1
vote
1 answer

Class-constrained protocols don't adopt MainActor

If I declare this protocol and model: protocol SubScene: UIViewController { func doSomething() } struct Model { let subScene: SubScene func longCalculation() async { // Some long-running operation... await subScene.doSomething() …
Rudedog
  • 4,323
  • 1
  • 23
  • 34
1
vote
1 answer

How to suspend subsequent tasks until first finishes then share its response with tasks that waited?

I have an actor which throttles requests in a way where the first one will suspend subsequent requests until finished, then share its response with them so they don't have to make the same request. Here's what I'm trying to do: let cache =…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
1 answer

SwifttUI with Core Data: load from private context

I am trying to display some objects in SwiftUI that I created using an NSManagedObjectContext set to a private queue (so that in case the user presses cancel, the objects aren't committed anywhere, basically the 'scratchpad' MOC). I initially create…
Z S
  • 7,039
  • 12
  • 53
  • 105
1
vote
1 answer

Swift and Core Data: calling 'await perform' inside another 'await perform'

I am using the iOS15 Swift + Core Data code for async/await, and while using private queue concurrency, I am trying to call a await perform block inside another await perform block: await privateMainContext.perform({ var displayName:…
Z S
  • 7,039
  • 12
  • 53
  • 105
1
vote
0 answers

How Do I Test an API With Swift Concurrency

I'm trying to test an API I'm writing with Structured Concurrency. I prefer to run my tests with continueAfterFailure = false so that I may establish preconditions on my tests. At the moment my test is: func testRssUrl() async throws { …
1
vote
2 answers

How to properly get rid of this concurrency compile error?

@MainActor class A {} class VC: UIViewController { let foo: A init() { self.foo = A() // Error: Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context super.init(nibName: nil, bundle: nil) } …
Steven Sorial
  • 121
  • 2
  • 9
1
vote
1 answer

How do I predictably serialise the order of execution some async code using the new Task api without using actors?

It looks like the Swift new concurrency model doesn't work well with the old one. I've tried to incrementally adopt the new swift concurrency model for my new async functions ( using the async/await ceremony) but I quickly hit a wall when the…
onthemoon
  • 3,302
  • 3
  • 17
  • 24
1
vote
1 answer

Is it safe to make a struct containing a closure Sendable?

I want to have a Sendable struct, which contains a closure. This closure takes in a reference type, but returns Void, therefore the Greeter doesn't directly stores the Person reference. However, closures themselves are still references…
George
  • 25,988
  • 10
  • 79
  • 133
1
vote
1 answer

Using try await with upload requests

I am using the following code for multipart data upload let postValue = try await AF.upload(multipartFormData: { multipartFormData in multipartFormData.append("123".data(using: .utf8, allowLossyConversion: false)!, withName: "number1") …
hariszaman
  • 8,202
  • 2
  • 40
  • 59
1
vote
1 answer

Retrieving records on a Reference List (hierarchical records) - Swift Concurrency & CloudKit

I am using CloudKit (the default public database) to store information about matches between two teams. I am trying to use the Swift Concurrent approach with async/await to retrieve the match and team data. The matches are stored as “Match”…
1
vote
1 answer

Can a Swift Concurrency Task be stored in a queue and started later?

I need to defer the execution of a task until I complete a high priority task, such as re-authenticating, then execute the original task from there. I'm trying to use Swift Concurrency's Task object for this: Task { await service.fetch(...) } I…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
1 answer

How to call original synchronous method that has async overload in Xcode 13

Recently switched to Xcode 13. I have an AVAsset writer, and trying to call the method writer.finishWriting() which now has an async version, as well as the synchronous version. I want to call the original synchronous version, but am getting the…
1
vote
0 answers

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) whenever I try to make a network request

I have been getting a weird crash whenever I run my app and use my request utilities. This is on XCode Beta 13.5 I should mention that I am using the Thread Sanitizer too. This error only appears with the Thread Sanitizer turned on. Here is the…
NduJay
  • 760
  • 1
  • 10
  • 23