Here is a new project that demonstrates the issue:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
/
do {
let context = persistentContainer.viewContext
let t = Test(context: context)
t.name = "H1"
try context.save()
} catch {}
}
func applicationWillTerminate(_ aNotification: Notification) {
/
}
/
lazy var persistentContainer: NSPersistentContainer = {
/
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "Test")
let description = NSPersistentStoreDescription()
description.type = NSSQLiteStoreType
container.persistentStoreDescriptions = [description]
description.setOption(MySpotlightDelegate(forStoreWith: description, model: container.managedObjectModel), forKey: NSCoreDataCoreSpotlightExporter)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
/
/
/
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error)")
}
})
return container
}()
/
@IBAction func saveAction(_ sender: AnyObject?) {
/
let context = persistentContainer.viewContext
if !context.commitEditing() {
NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing before saving")
}
if context.hasChanges {
do {
try context.save()
} catch {
/
let nserror = error as NSError
NSApplication.shared.presentError(nserror)
}
}
}
func windowWillReturnUndoManager(window: NSWindow) -> UndoManager? {
/
return persistentContainer.viewContext.undoManager
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
/
let context = persistentContainer.viewContext
if !context.commitEditing() {
NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing to terminate")
return .terminateCancel
}
if !context.hasChanges {
return .terminateNow
}
do {
try context.save()
} catch {
let nserror = error as NSError
/
let result = sender.presentError(nserror)
if (result) {
return .terminateCancel
}
let question = NSLocalizedString("Could not save changes while quitting. Quit anyway?", comment: "Quit without saves error question message")
let info = NSLocalizedString("Quitting now will lose any changes you have made since the last successful save", comment: "Quit without saves error question info");
let quitButton = NSLocalizedString("Quit anyway", comment: "Quit anyway button title")
let cancelButton = NSLocalizedString("Cancel", comment: "Cancel button title")
let alert = NSAlert()
alert.messageText = question
alert.informativeText = info
alert.addButton(withTitle: quitButton)
alert.addButton(withTitle: cancelButton)
let answer = alert.runModal()
if answer == .alertSecondButtonReturn {
return .terminateCancel
}
}
/
return .terminateNow
}
}
private class MySpotlightDelegate: NSCoreDataCoreSpotlightDelegate {
}
And the errors from this are:
2017-09-19 08:24:38.947868+0100 Test[48575:5222926] [index] IndexConnection invalid: Connection invalid
2017-09-19 08:24:38.952997+0100 Test[48575:5222926] [error] error: Error indexing (delete): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
CoreData: error: Error indexing (delete): Error Domain=CSIndexErrorDomain Code=-1 "(null)"