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
How to Use a Value Type Object as a Reference Type
Swift "Is" Operator with Type Stored in Variable
How to I Turn Off the Ambient Light in Scene Kit (With Swift)
Make a Type Itself -- Not Its Instances -- Conform to a Protocol
How to I Access Argv and Argc in Swift
How to Trim a String in Swift Based on a Character
Using Vapor-Fluent to Upsert Models
How to Make Sfspeechrecognizer Available on MACos
Building an Nsoutline View with Check Marks
How to Check If Airpods Are Connected to Iphone
Swift Protocol for String Interpolation
Swift Combine Publishers VS Completion Handler and When to Cancel
Need Detailed Explanation for Memoize Implementation in Swift (Wwdc 14, Session 404)
Require Associatedtype to Be Representable in a @Convention(C) Block
How to Suppress Warnings in Swift 3
How to Prevent a Spacer to Make a VStack Greedly Grow Beyond Necessary