@FPST I had to write a method that returned the models from a sorted fetch and then get their PersistentIdentifiers.
public func fetchSort<T: PersistentModel>(_ descriptor: FetchDescriptor<T>) throws -> [PersistentIdentifier] {
var result = [PersistentIdentifier]()
var models = [T]()
models = try self.modelContext.fetch(descriptor)
for model in models {
let pID = model.persistentModelID
result.append(pID)
}
return result
}
let fetchDescriptor = FetchDescriptor() works fine, but
let fetchDescriptor = FetchDescriptor<MyModel>(sortBy: [SortDescriptor( \.title)])
fails with
The operation couldn’t be completed. (SwiftData.SwiftDataError error 1.)
I have two screenshots of the threads, one under Swift 5 and one under Swift 6. I set a breakpoint in the updateView method. The Swift 5 run gets to the breakpoint. The Swift 6 run does not and crashes.
Swift 5
Swift 6
Any help would be most appreciated. Or maybe this is ripe for a bug report?
I've discovered where the crash is occurring. I have an @objc function called updateView() that is fired when the view controller detects NSPersistenStoreRemoteChange. As soon as I save an object in another view, the update fires and the app crashes.
NotificationCenter.default.addObserver(self, selector: #selector(updateView(_ :)), name: .NSPersistentStoreRemoteChange, object: nil)
The crash does not occur using Swift 5.
I now understand the the warnings go away under Swift 6 as 'InferSendableFromCaptures' has been enabled. I still have the crash. Guess I better dig in.
@DTS Engineer, here's an example of what I did.
nonisolated func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
Task {
await changeMyLocation(locations)
}
}
func changeMyLocation(_ locations: [CLLocation]) {
myLocation = locations.last
}
I created a function to change the location and called it via a task.
I've fixed this by creating a struct for the data returned by SwiftData. I stuff the struct with the SwiftData and use the struct in my UICollectionViewDiffableDataSource. I am using SwiftData with UIKit.
@FPST I had to write a method that returned the models from a sorted fetch and then get their PersistentIdentifiers.
public func fetchSort<T: PersistentModel>(_ descriptor: FetchDescriptor<T>) throws -> [PersistentIdentifier] {
var result = [PersistentIdentifier]()
var models = [T]()
models = try self.modelContext.fetch(descriptor)
for model in models {
let pID = model.persistentModelID
result.append(pID)
}
return result
}
let fetchDescriptor = FetchDescriptor() works fine, but
let fetchDescriptor = FetchDescriptor<MyModel>(sortBy: [SortDescriptor( \.title)])
fails with
The operation couldn’t be completed. (SwiftData.SwiftDataError error 1.)
I have two screenshots of the threads, one under Swift 5 and one under Swift 6. I set a breakpoint in the updateView method. The Swift 5 run gets to the breakpoint. The Swift 6 run does not and crashes.
Swift 5
Swift 6
Any help would be most appreciated. Or maybe this is ripe for a bug report?
I've discovered where the crash is occurring. I have an @objc function called updateView() that is fired when the view controller detects NSPersistenStoreRemoteChange. As soon as I save an object in another view, the update fires and the app crashes.
NotificationCenter.default.addObserver(self, selector: #selector(updateView(_ :)), name: .NSPersistentStoreRemoteChange, object: nil)
The crash does not occur using Swift 5.
I now understand the the warnings go away under Swift 6 as 'InferSendableFromCaptures' has been enabled. I still have the crash. Guess I better dig in.
@DTS Engineer, here's an example of what I did.
nonisolated func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
Task {
await changeMyLocation(locations)
}
}
func changeMyLocation(_ locations: [CLLocation]) {
myLocation = locations.last
}
I created a function to change the location and called it via a task.
I've fixed this by creating a struct for the data returned by SwiftData. I stuff the struct with the SwiftData and use the struct in my UICollectionViewDiffableDataSource. I am using SwiftData with UIKit.