I have an app which uses Core Data with an entity called Item
and has the attribute "url" to save URL
.
The FetchRequest looks like the code below.
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: Item.url, ascending: true)], animation: .default)
The app crashes when creating a item
let newItem = Item(context: viewContext)
newItem.url = URL(string: "https://www.google.com") //crashes after this line
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error (nsError), (nsError.userInfo)")
}
with this crash log
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL compare:]: unrecognized selector sent to instance 0x2838187e0'
When sorting using other attributes(ex. String
) doesn't cause the app to crash.
I have also tried implementing fetchrequest from init()
var fetchedItem: FetchRequest<Item>
init(){
fetchedItem = FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: Item.url, ascending: true)], animation: .default)
}
Changing the sort from string to url works, only crashes when creating a new item to save.
Is this a wrong way for implementing? Or is it just not possible to use FetchRequest and Sort by URL together?
question from:
https://stackoverflow.com/questions/66056599/sorting-urls-with-fetchrequest-crashes-when-new-content-is-saved 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…