We are getting a crash _dispatch_assert_queue_fail when the cancellationHandler on NSProgress is called.
We do not see this with iOS 17.x, only on iOS 18. We are building in Swift 6 language mode and do not have any compiler warnings.
We have a type whose init looks something like this:
    init(
        request: URLRequest,
        destinationURL: URL,
        session: URLSession
    ) {
        progress = Progress()
        progress.kind = .file
        progress.fileOperationKind = .downloading
        progress.fileURL = destinationURL
        
        progress.pausingHandler = { [weak self] in
            self?.setIsPaused(true)
        }
        
        progress.resumingHandler = { [weak self] in
            self?.setIsPaused(false)
        }
        
        progress.cancellationHandler = { [weak self] in
            self?.cancel()
        }
When the progress is cancelled, and the cancellation handler is invoked. We get the crash. The crash is not reproducible 100% of the time, but it happens significantly often. Especially after cleaning and rebuilding and running our tests.
* thread #4, queue = 'com.apple.root.default-qos', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18017b0e8)
  * frame #0: 0x000000018017b0e8 libdispatch.dylib`_dispatch_assert_queue_fail + 116
    frame #1: 0x000000018017b074 libdispatch.dylib`dispatch_assert_queue + 188
    frame #2: 0x00000002444c63e0 libswift_Concurrency.dylib`swift_task_isCurrentExecutorImpl(swift::SerialExecutorRef) + 284
    frame #3: 0x000000010b80bd84 MyTests`closure #3 in MyController.init() at MyController.swift:0
    frame #4: 0x000000010b80bb04 MyTests`thunk for @escaping @callee_guaranteed @Sendable () -> () at <compiler-generated>:0
    frame #5: 0x00000001810276b0 Foundation`__20-[NSProgress cancel]_block_invoke_3 + 28
    frame #6: 0x00000001801774ec libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #7: 0x0000000180178de0 libdispatch.dylib`_dispatch_client_callout + 16
    frame #8: 0x000000018018b7dc libdispatch.dylib`_dispatch_root_queue_drain + 1072
    frame #9: 0x000000018018bf60 libdispatch.dylib`_dispatch_worker_thread2 + 232
    frame #10: 0x00000001012a77d8 libsystem_pthread.dylib`_pthread_wqthread + 224
Any thoughts on why this is crashing and what we can do to work-around it? I have not been able to extract our code into a simple reproducible case yet. And I mostly see it when running our code in a testing environment (XCTest). Although I have been able to reproduce it running an app a few times, it's just less common.
                    
                  
                Concurrency
RSS for tagConcurrency is the notion of multiple things happening at the same time.
Posts under Concurrency tag
            
              
                133 Posts
              
            
            
              
                
              
            
          
          
  
    
    Selecting any option will automatically load the page
  
  
  
  
    
  
  
              Post
Replies
Boosts
Views
Activity
                    
                      I get many warnings like this when I build an old project.
I asked AI chatbot which gave me several solutions, the recommended one is:
var hashBag = [String: Int]()
func updateHashBag() async {
    var tempHashBag = hashBag // make copy
    await withTaskGroup(of: Void.self) { group in
        group.addTask {
            tempHashBag["key1"] = 1
        }
        group.addTask {
            tempHashBag["key2"] = 2
        }
    }
    hashBag = tempHashBag // copy back?
}
My understanding is that in the task group, the concurrency engine ensures synchronized modifications on the temp copy in multiple tasks. I should not worry about this.
My question is about performance.
What if I want to put a lot of data into the bag? Does the compiler do some kind of magics to optimize low level memory allocations? For example, the temp copy actually is not a real copy, it is a special reference to the original hash bag; it is only grammar glue that I am modifying the copy.
                    
                  
                
                    
                      Hi Apple Developer Community,
I'm facing a crash when updating an array of tuples from both a background thread and the main thread simultaneously. Here's a simplified version of the code in a macOS app using AppKit:
class ViewController: NSViewController {
    var mainthreadButton = NSButton(title: "test", target: self, action: nil)
    var numbers = Array(repeating: (dim: Int, key: String)(0, "default"), count: 1000)
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(mainthreadButton)
        mainthreadButton.translatesAutoresizingMaskIntoConstraints = false
        mainthreadButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        mainthreadButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        mainthreadButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
        mainthreadButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
        mainthreadButton.target = self
        mainthreadButton.action = #selector(arraytest(_:))
    }
    @objc func arraytest(_ sender: NSButton) {
        print("array update started")
        // Background update
        DispatchQueue.global().async {
            for i in 0..<1000 {
                self.numbers[i].dim = i
            }
        }
        // Main thread update
        var sum = 0
        for i in 0..<1000 {
            numbers[i].dim = i + 1
            sum += numbers[i].dim
            print("test \(sum)")
        }
        mainthreadButton.title = "test = \(sum)"
    }
}
This results in a crash with the following message:
malloc: double free for ptr 0x136040c00
malloc: *** set a breakpoint in malloc_error_break to debug
What's interesting:
This crash only happens when the tuple contains a String ((dim: Int, key: String))
If I change the tuple type to use two Int values ((dim: Int, key: Int)), the crash does not occur
My Questions:
Why does mutating an array of tuples containing a String crash when accessed from multiple threads?
Why is the crash avoided when the tuple contains only primitive types like Int?
Is there an underlying memory management issue with value types containing reference types like String?
Any explanation about this behavior and best practices for thread-safe mutation of such arrays would be much appreciated.
Thanks in advance!
                    
                  
                
                    
                      Hello,
I am developing an application which is communicating with external device using BLE and L2CAP. I wonder what are the best practices of using Input & Output streams that are established with L2CAP connection when working with Swift 6 concurrency model.
I've been trying to find some examples and hints for some time now but unfortunately there isn't much available. One useful thread I've found is: https://developer.apple.com/forums/thread/756281
but it does not offer much insight into using eg. actor model with streams. I wonder if something has changed in this regards?
Also, are there any plans to migrate eg. CoreBluetooth stack to new swift 6 concurrency ?
                    
                  
                
              
                
              
              
                
                Topic:
                  
	
		App & System Services
  	
                
                
                SubTopic:
                  
                    
	
		Processes & Concurrency
		
  	
                  
                
              
              
                Tags:
              
              
  
  
    
      
      
      
        
          
            External Accessory
          
        
        
      
      
    
      
      
      
        
          
            Swift
          
        
        
      
      
    
      
      
      
        
          
            Core Bluetooth
          
        
        
      
      
    
      
      
      
        
          
            Concurrency
          
        
        
      
      
    
  
  
              
                
                
              
            
          
                    
                      How can I return the results of a Spotlight query synchronously from a Swift function?
I want to return a [String] that contains the items that match the query, one item per array element.
I specifically want to find all data for Spotlight items in the /Applications folder that have a kMDItemAppStoreAdamID (if there is a better predicate than kMDItemAppStoreAdamID > 0, please let me know).
The following should be the correct query:
let query = NSMetadataQuery()
query.predicate = NSPredicate(format: "kMDItemAppStoreAdamID > 0")
query.searchScopes = ["/Applications"]
I would like to do this for code that can run on macOS 10.13+, which precludes using Swift Concurrency. My project already uses the latest PromiseKit, so I assume that the solution should use that. A bonus solution using Swift Concurrency wouldn't hurt as I will probably switch over sometime in the future, but won't be able to switch soon.
I have written code that can retrieve the Spotlight data as the [String], but I don't know how to return it synchronously from a function; whatever I tried, the query hangs, presumably because I've called various run loop functions at the wrong places.
In case it matters, the app is a macOS command-line app using Swift 5.7 & Swift Argument Parser 1.5.0. The Spotlight data will be output only as text to stdout & stderr, not to any Apple UI elements.
                    
                  
                
                    
                      Hi all,
I'm running into a Swift Concurrency issue and would appreciate some help understanding what's going on.
I have a protocol and an actor set up like this:
protocol PersistenceListener: AnyObject {
    func persistenceDidUpdate(key: String, newValue: Any?)
}
actor Persistence {
    
    func addListener(_ listener: PersistenceListener) {
        listeners.add(listener)
    }
    
    /// Removes a listener.
    func removeListener(_ listener: PersistenceListener) {
        listeners.remove(listener)
    }
    
    // MARK: - Private Properties
    private var listeners = NSHashTable<AnyObject>.weakObjects()
    
    // MARK: - Private Methods
    
    /// Notifies all registered listeners on the main actor.
    private func notifyListeners(key: String, value: Any?) async {
        let currentListeners = listeners.allObjects.compactMap { $0 as? PersistenceListener }
        for listener in currentListeners {
            await MainActor.run {
                listener.persistenceDidUpdate(key: key, newValue: value)
            }
        }
    }
}
When I compile this code, I get a concurrency error:
"Sending 'listener' risks causing data races"
                    
                  
                
                    
                      I've been working on Swift game which is not yet launched or available for preview.
The game works in such a way that it has idle CPU while the user is thinking and sustained max CPU and GPU on as many cores as possible when he makes a move.
Rarely, due to OS activity or something else outside of my control (for example when dropping the OS curtain even if for just a bit then remove it), the game or some of its threads are moved to efficiency cores which results in major stuttering which persists precisely until the game is idle again at which point the game is moved back on performance cores - but if the player keeps making moves the stuttering simply won't go away and so I guess compuptation is locked onto efficiency cores.
The issue does not reproduce on MacCatalyst on Intel.
How do I tell Swift to avoid efficiency cores?
BTW Swift and SceneKIT have AMAZING performance especially when compared to others.
                    
                  
                
                    
                      I'm continuing with the migration towards Swift 6. Within one of our libraries, I want to check whether a parameter object: Any? confirms to Sendable.
I tried the most obvious one:
if let sendable = object as? Sendable {
}
But that results into the compiler error "Marker protocol 'Sendable' cannot be used in a conditional cast".
Is there an other way to do this?
                    
                  
                
                    
                      Hi,
I'm trying the new Swift Testing instead of XCTest for my new project. I am using RxSwift+UIKit. And when I am trying to test my ViewModel that has a Driver in it, it crashes due to the driver is not being called form main thread.
Thread 5: Fatal error: `drive*` family of methods can be only called from `MainThread`.
Here is the test code:
struct PlayerViewModelTest {
    @Test func testInit_shouldPopulateTable_withEmpty() async throws {
        // Arrange
        let disposeBag = DisposeBag()
        var expectedSongTableCellViewData: [SongTableCellViewData]?
        
        // Act
        let sut = PlayerViewModel(provideAllSongs: { return .just(mockSongList)  },
                                  provideSongByArtist: { _ in return .just(mockSongList) },
                                  disposeBag: disposeBag)
        
        sut.populateTable
            .drive(onNext: { expectedSongTableCellViewData = $0 })
            .disposed(by: disposeBag)
        
        // Assert
        #expect(expectedSongTableCellViewData != nil, "Should emit something so it should not be nil")
        #expect(expectedSongTableCellViewData!.isEmpty, "Should emit empty array")
    }
    
}
This never happen in XCTest. So I assume Swift Testing is not being run in the main thread? How do I fix this?
Thanks
                    
                  
                
                    
                      Hello,
I recently published an app that uses Swift Data as its primary data storage. The app uses concurrency, background threads, async await, and BLE communication.
Sadly, I see my app incurs many fringe crashes, involving EXC_BAD_ACCESS, KERN_INVALID_ADDRESS, EXC_BREAKPOINT, etc.
I followed these guidelines:
One ModelContainer that is stored as a global variable and used throughout.
ModelContexts are created separately for each task, changes are saved manually, and models are not passed around.
Threads with different ModelContexts might manipulate and/or read the same data simultaneously.
I was under the impression this meets the usage requirements.
I suspect perhaps the issue lies in my usage of contexts in a single await function, that might be paused and resumed on a different thread (although same execution path). Is that the case? If so, how should SwiftData be used in async scopes?
Is there anything else particularly wrong in my approach?
                    
                  
                
                    
                      I ran into a problem, I have a recursive function in which Data type objects are temporarily created, because of this, the memory expands until the entire recursion ends. It would just be fixed using autoreleasepool, but it can't be used with async await, and I really don't want to rewrite the code for callbacks. Is there any option to use autoreleasepool with async await functions? (I Googled one option, that the Task already contains its own autoreleasepool, and if you do something like that, it should work, but it doesn't, the memory is still growing)
func autoreleasepool<Result>(_ perform: @escaping () async throws -> Result) async throws -> Result {
    try await Task {
        try await perform()
    }.value
}
                    
                  
                
                    
                      As of iOS 18.3 SDK, Core Bluetooth is still mostly an Objective-C framework: key objects like CBPeripheral inherit from NSObjectProtocol and does not conform to Sendable.
CBCentralManager has a convenience initializer that allows the caller to provide a dispatch_queue for delegate callbacks. I want my Swift package that implements Core Bluetooth to conform to Swift 6 strict concurrency checking.
It is unsafe to dispatch the delegate events onto my own actor, as the passed in objects are presumably not thread-safe. What is the recommended concurrency safe way to implement Core Bluetooth in Swift 6 with strict concurrency checking enabled?
                    
                  
                
                    
                      In Swift 6, stricter concurrency rules can lead to challenges when making SwiftUI views conform to Equatable. Specifically, the == operator required for Equatable must be nonisolated, which means it cannot access @MainActor-isolated properties. This creates an error when trying to compare views with such properties:
Error Example:
struct MyView: View, Equatable {
    let title: String  
    let count: Int
    static func ==(lhs: MyView, rhs: MyView) -> Bool {
        // Accessing `title` here would trigger an error due to actor isolation.
        return lhs.count == rhs.count
    }
    var body: some View {
        Text(title)
    }
}
Error Message:
Main actor-isolated operator function '==' cannot be used to satisfy nonisolated protocol requirement; this is an error in the Swift 6 language mode.
Any suggestions?
Thanks
FB: FB15753655 (SwiftUI View cannot conform custom Equatable protocol in Swift 6.)
                    
                  
                
                    
                      I’m trying to create a property wrapper that that can manage shared state across any context, which can get notified if changes happen from somewhere else.
I'm using mutex, and getting and setting values works great. However, I can't find a way to create an observer pattern that the property wrappers can use.
The problem is that I can’t trigger a notification from a different thread/context, and have that notification get called on the correct thread of the parent object that the property wrapper is used within.
I would like the property wrapper to work from anywhere: a SwiftUI view, an actor, or from a class that is created in the background. The notification preferably would get called synchronously if triggered from the same thread or actor, or otherwise asynchronously. I don’t have to worry about race conditions from the notification because the state only needs to reach eventuall consistency.
Here's the simplified pseudo code of what I'm trying to accomplish:
// A single source of truth storage container.
final class MemoryShared<Value>: Sendable {
    let state = Mutex<Value>(0)
    func withLock(_ action: (inout Value) -> Void) {
        state.withLock(action)
        notifyObservers()
    }
    func get() -> Value
    func notifyObservers()
    func addObserver()
}
// Some shared state used across the app
static let globalCount = MemoryShared<Int>(0)
// A property wrapper to access the shared state and receive changes
@propertyWrapper
struct SharedState<Value> {
    public var wrappedValue: T {
        get { state.get() }
        nonmutating set { // Can't set directly }
    }
    var publisher: Publisher {}
    init(state: MemoryShared) {
        // ...
    }
}
// I'd like to use it in multiple places:
@Observable
class MyObservable {
    @SharedState(globalCount)
    var count: Int
}
actor MyBackgroundActor {
    @SharedState(globalCount)
    var count: Int
}
@MainActor
struct MyView: View {
    @SharedState(globalCount)
    var count: Int
}
What I’ve Tried
All of the examples below are using the property wrapper within a @MainActor class. However the same issue happens no matter what context I use the wrapper in: The notification callback is never called on the context the property wrapper was created with.
I’ve tried using @isolated(any) to capture the context of the wrapper and save it to be called within the state in with unchecked sendable, which doesn’t work:
final class MemoryShared<Value: Sendable>: Sendable {
    // Stores the callback for later.
    public func subscribe(callback: @escaping @isolated(any) (Value) -> Void) -> Subscription 
}
@propertyWrapper
struct SharedState<Value> {
    init(state: MemoryShared<Value>) {
        MainActor.assertIsolated() // Works!
        state.subscribe {
            MainActor.assertIsolated() // Fails
            self.publisher.send()
        }
    }
}
I’ve tried capturing the isolation within a task with AsyncStream. This actually compiles with no sendable issues, but still fails:
@propertyWrapper
struct SharedState<Value> {    
    init(isolation: isolated (any Actor)? = #isolation, state: MemoryShared<Value>) {
        let (taskStream, continuation) = AsyncStream<Value>.makeStream()
        // The shared state sends new values to the continuation. 
        subscription = state.subscribe(continuation: continuation)
        
        MainActor.assertIsolated() // Works!
        
        let task = Task {
            _ = isolation
            for await value in taskStream {
                _ = isolation
                MainActor.assertIsolated() // Fails
            }
        }
    }
}
I’ve tried using multiple combine subjects and publishers:
final class MemoryShared<Value: Sendable>: Sendable {
    let subject: PassthroughSubject<T, Never> // ...
    var publisher: Publisher {} // ...
}
@propertyWrapper
final class SharedState<Value> {
    var localSubject: Subject
    init(state: MemoryShared<Value>) {
        MainActor.assertIsolated() // Works!
        handle = localSubject.sink {
            MainActor.assertIsolated() // Fails
        }
        stateHandle = state.publisher.subscribe(localSubject)
    }
}
I’ve also tried:
Using NotificationCenter
Making the property wrapper a class
Using NSKeyValueObserving
Using a box class that is stored within the wrapper.
Using @_inheritActorContext.
All of these don’t work, because the event is never called from the thread the property wrapper resides in.
Is it possible at all to create an observation system that notifies the observer from the same context as where the observer was created?
Any help would be greatly appreciated!
                    
                  
                
                    
                      In my app, I've been using ModelActors in SwiftData, and using actors with a custom executor in Core Data to create concurrency safe services.
I have multiple actor services that relate to different data model components or features, each that have their own internally managed state (DocumentService, ImportService, etc).
The problem I've ran into, is that I need to be able to use multiple of these services within another service, and those services need to share the same context. Swift 6 doesn't allow passing contexts across actors.
The specific problem I have is that I need a master service that makes multiple unrelated changes without saving them to the main context until approved by the user.
I've tried to find a solution in SwiftData and Core Data, but both have the same problem which is contexts are not sendable. Read the comments in the code to see the issue:
/// This actor does multiple things without saving, until committed in SwiftData.
@ModelActor
actor DatabaseHelper {
    func commitChange() throws {
        try modelContext.save()
    }
    
    func makeChanges() async throws {
        // Do unrelated expensive tasks on the child context...
        
        // Next, use our item service
        let service = ItemService(modelContainer: SwiftDataStack.shared.container)
        let id = try await service.expensiveBackgroundTask(saveChanges: false)
        
        // Now that we've used the service, we need to access something the service created.
        // However, because the service created its own context and it was never saved, we can't access it.
       let itemFromService = context.fetch(id) // fails
        
        // We need to be able to access changes made from the service within this service, 
        /// so instead I tried to create the service by passing the current service context, however that results in:
        // ERROR: Sending 'self.modelContext' risks causing data races
        let serviceFromContext = ItemService(context: modelContext)
        
        // Swift Data doesn't let you create child contexts, so the same context must be used in order to change data without saving. 
    }
}
@ModelActor
actor ItemService {
    init(context: ModelContext) {
        modelContainer = SwiftDataStack.shared.container
        modelExecutor = DefaultSerialModelExecutor(modelContext: context)
    }
    
    func expensiveBackgroundTask(saveChanges: Bool = true) async throws -> PersistentIdentifier? {
        // Do something expensive...
        return nil
    }
}
Core Data has the same problem:
/// This actor does multiple things without saving, until committed in Core Data.
actor CoreDataHelper {
    let parentContext: NSManagedObjectContext
    let context: NSManagedObjectContext
    
    /// In Core Data, I can create a child context from a background context.
    /// This lets you modify the context and save it without updating the main context.
    init(progress: Progress = Progress()) {
        parentContext = CoreDataStack.shared.newBackgroundContext()
        let childContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        childContext.parent = parentContext
        self.context = childContext
    }
    
    /// To commit changes, save the parent context pushing them to the main context.
    func commitChange() async throws {
        // ERROR: Sending 'self.parentContext' risks causing data races
        try await parentContext.perform {
            try self.parentContext.save()
        }
    }
    func makeChanges() async throws {
        // Do unrelated expensive tasks on the child context...
        // As with the Swift Data example, I am unable to create a service that uses the current actors context from here.
        // ERROR: Sending 'self.context' risks causing data races
        let service = ItemService(context: self.context)
    }
}
Am I going about this wrong, or is there a solution to fix these errors?
Some services are very large and have their own internal state. So it would be very difficult to merge all of them into a single service. I also am using Core Data, and SwiftData extensively so I need a solution for both.
I seem to have trapped myself into a corner trying to make everything concurrency save, so any help would be appreciated!
                    
                  
                
                    
                      Hello, I was hoping to clarify my understanding of the use of for await with an AsyncStream. My use case is, I'd like to yield async closures to the stream's continuation, with the idea that, when I use for await with the stream to process and execute the closures, it would only continue on to the following closure once the current closure has been run to completion.
At a high level, I am trying to implement in-order execution of async closures in the context of re-entrancy. An example of asynchronous work I want to execute is a network call that should write to a database:
func syncWithRemote() async -> Void {
    let data = await fetchDataFromNetwork()
    await writeToLocalDatabase(data)
}
For the sake of example, I'll call the intended manager of closure submission SingleOperationRunner.
where, at a use site such as this, my desired outcome is that call 1 of syncWithRemote() is always completed before call 2 of it:
let singleOperationRunner = SingleOperationRunner(priority: nil)
singleOperationRunner.run {
    syncWithRemote()
}
singleOperationRunner.run {
    syncWithRemote()
}
My sketch implementation looks like this:
public final class SingleOperationRunner {
    private let continuation: AsyncStream<() async -> Void>.Continuation
    
    public init(priority: TaskPriority?) {
        let (stream, continuation) = AsyncStream.makeStream(of: (() async -> Void).self)
        
        self.continuation = continuation
        
        Task.detached(priority: priority) {
            // Will this loop only continue when the `await operation()` completes?
            for await operation in stream {
                await operation()
            }
        }
    }
    
    public func run(operation: @escaping () async -> Void) {
        continuation.yield(operation)
    }
    
    deinit {
        continuation.finish()
    }
}
The resources I've found are https://developer.apple.com/videos/play/wwdc2022-110351/?time=1445 and https://forums.swift.org/t/swift-async-func-to-run-sequentially/60939/2 but do not think I have fully put the pieces together, so would appreciate any help!
                    
                  
                
                    
                      Consider this simple miniature of my iOS Share Extension:
import SwiftUI
import Photos
class ShareViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        if let itemProviders = (extensionContext?.inputItems.first as? NSExtensionItem)?.attachments {
            let hostingView = UIHostingController(rootView: ShareView(extensionContext: extensionContext, itemProviders: itemProviders))
            hostingView.view.frame = view.frame
            view.addSubview(hostingView.view)
        }
        
    }
}
struct ShareView: View {
    var extensionContext: NSExtensionContext?
    var itemProviders: [NSItemProvider]
    
    var body: some View {
        VStack{}
        .task{
            await extractItems()
        }
    }
    
    func extractItems() async {
        guard let itemProvider = itemProviders.first else { return }
        guard itemProvider.hasItemConformingToTypeIdentifier(UTType.url.identifier) else { return }
        do {
            guard let url = try await itemProvider.loadItem(forTypeIdentifier: UTType.url.identifier) as? URL else { return }
            try await downloadAndSaveMedia(reelURL: url.absoluteString)
            extensionContext?.completeRequest(returningItems: [])
            
        }
        catch {}
    }
}
On the line 34
guard let url = try await itemProvider.loadItem
...
I get these warnings:
Passing argument of non-sendable type '[AnyHashable : Any]?' outside of main actor-isolated context may introduce data races; this is an error in the Swift 6 language mode
1.1. Generic enum 'Optional' does not conform to the 'Sendable' protocol (Swift.Optional)
Passing argument of non-sendable type 'NSItemProvider' outside of main actor-isolated context may introduce data races; this is an error in the Swift 6 language mode
2.2. Class 'NSItemProvider' does not conform to the 'Sendable' protocol (Foundation.NSItemProvider)
How to fix them in Xcode 16?
Please provide a solution which works, and not the one which might (meaning you run the same code in Xcode, add your solution and see no warnings).
I tried
Decorating everything with @MainActors
Using @MainActor in the .task
@preconcurrency import
Decorating everything with @preconcurrency
Playing around with nonisolated
                    
                  
                
                    
                      I'm trying to rewrite a Swift code to Swift 6 language mode and am stuck with this problem. How do I safely pass the bestAttemptContent and contentHandler to the Task? This is from the UNNotificationServiceExtension subclass.
final class NotificationService: UNNotificationServiceExtension {
    
      var contentHandler: ((UNNotificationContent) -> Void)?
      var bestAttemptContent: UNMutableNotificationContent?
      var customNotificationTask: Task<Void, Error>?
    
      override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        guard let bestAttemptContent = bestAttemptContent else {
    
          invokeContentHandler(with: request.content)
          return
        }
    
        do {
          let notificationModel = try PushNotificationUserInfo(data: request.content.userInfo)
          guard let templatedImageUrl = notificationModel.templatedImageUrlString,
                let imageUrl = imageUrl(from: templatedImageUrl) else {
            invokeContentHandler(with: bestAttemptContent)
            return
          }
          setupCustomNotificationTask(
            imageUrl: imageUrl,
            bestAttemptContent: bestAttemptContent,
            contentHandler: contentHandler
          )
        } catch {
          invokeContentHandler(with: bestAttemptContent)
        }
      }
    
      // More code
    
      private func downloadImageTask(
            imageUrl: URL,
            bestAttemptContent: UNMutableNotificationContent,
            contentHandler: @escaping (UNNotificationContent) -> Void
      ) {
            self.customNotificationTask = Task {
              let (location, _) = try await URLSession.shared.download(from: imageUrl)
              let desiredLocation = URL(fileURLWithPath: "\(location.path)\(imageUrl.lastPathComponent)")
              
                try FileManager.default.moveItem(at: location, to: desiredLocation)
                let attachment = try UNNotificationAttachment(identifier: imageUrl.absoluteString, url: desiredLocation, options: nil)
              bestAttemptContent.attachments = [attachment]
              contentHandler(bestAttemptContent)
            }
       }
    }
I tried using the MainActor.run {}, but it just moved the error to that run function.
The UNNotificationRequest is not sendable, and I don't think I can make it so.
Wrap the setupCustomNotification in a Task will move the errors to the didReceive method.
It seems like the consuming keyword will help here, but it leads to a compilation error, even with the latest Xcode (16.2).
Any pointers?
                    
                  
                
                    
                      Hello ladies and gentlemen, I'm writing a simple renderer on the main actor using Metal and Swift 6. I am at the stage now where I want to create a render pipeline state using asynchronous API:
@MainActor
class Renderer {
    let opaqueMeshRPS: MTLRenderPipelineState
    init(/*...*/) async throws {
        let descriptor = MTLRenderPipelineDescriptor()
        // ...
        
        opaqueMeshRPS = try await device.makeRenderPipelineState(descriptor: descriptor)
    }
}
I get a compilation error if try to use the asynchronous version of the makeRenderPipelineState method:
Non-sendable type 'any MTLRenderPipelineState' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary
Which is understandable, since MTLRenderPipelineState is not Sendable. But it looks like no matter where or how I try to access this method, I just can't do it - you have this API, but you can't use it, you can only use the synchronous versions.
Am I missing something or is Metal just not usable with Swift 6 right now?
                    
                  
                
                    
                      Hello,
When attempting to assign the UNNotificationResponse to a Published property on the main thread inside UNUserNotificationCenterDelegate's method
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async
both Task { @MainActor in } and await MainActor.run are throwing a  NSInternalInconsistencyException: 'Call must be made on main thread'.
I thought both of them were essentially doing the same thing, i.e. call their closure on the main thread. So why is this exception thrown? Is my understanding of the MainActor still incorrect, or is this a bug?
Thank you
Note: Task { await MainActor.run { ... } } and DispatchQueue.main.async don't throw any exception.