2021年2月4日星期四

Sorting URLs with FetchRequest crashes when new content is saved

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)  }  

Is this a wrong way for implementing? Or is it just not possible to use FetchRequest and Sort by URL together?

https://stackoverflow.com/questions/66056599/sorting-urls-with-fetchrequest-crashes-when-new-content-is-saved February 05, 2021 at 10:06AM

没有评论:

发表评论