Using @Fetchrequest(Entity: ) for Swiftui MACos App Crashes

NSPersistentDocument FetchRequest warp property crash on macOS Document App SwiftUI project

This is due to emptiness of new document. As in any document-based application you have to prepare some default initial data for new document

Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

in Document.swift

class Document: NSPersistentDocument {

// .. other code here

override func makeWindowControllers() {

// in case of new document create new empty book in context
// that will be shown in opened document window
let isNew = self.fileURL == nil
if isNew {
_ = Book(context: self.managedObjectContext!) // << here !!
}

let contentView = ContentView().environment(\.managedObjectContext, self.managedObjectContext!)

// ... other code here

SwiftUI @FetchRequest crashes the app and returns error

In the @enviromentVAr, sometimes, you need to set the enviroment by yourself.

 let managedObjectContext: NSManagedObjectContext =  ((UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!

ContentView().environment(\.managedObjectContext, managedObjectContext)

Then the managedObjectContext can work.

 @Environment(\.managedObjectContext) var managedObjectContext

SwiftUI with Core Data: Fetch request with predicate crashes

Thanks to andrewbuilder. He got me looking in the right direction. The correct way to use a predicate that expects an Int is:

predicate: NSPredicate(format: "type == %i", Int16(1)))

I was using %@ when I should have used %i.

FetchRequest for 12000 entries extremely slow on macOS app but not iOS app - despite code being the same (SwiftUI on macOS Big Sur / iOS 14)

I solved this issue; it seems that it was indeed the warning "'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release" which caused this spike in CPU usage. After setting "NSSecureUnarchiveFromDataTransformer" to all [String]-Transformable-Properties of all database entities, the issue was gone.

Sorting URLs with FetchRequest crashes when new content is saved

Please look closely at the error message

'-[NSURL compare:]: unrecognized selector sent

and please look also at the NSSortDescriptor documentation

You construct instances of NSSortDescriptor by specifying the key path of the property to be compared and the order of the sort (ascending or descending). Optionally, you can also specify a selector to use to perform the comparison, which allows you to specify other comparison selectors such as localizedStandardCompare: and localizedCaseInsensitiveCompare:. Sorting raises an exception if the objects to be sorted do not respond to the sort descriptor’s comparison selector

So the error is obvious: NSURL is not comparable. You need a string like description, absoluteString or just the host (the google.com part)


One solution is to create an extension of (NS)URL like in Asperi's answer.

An easier solution is to create the NSSortDescriptor with a string key (path) rather than a Swift key path.

@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "url.host", ascending: true)], animation: .default)


Related Topics



Leave a reply



Submit