IndexConnection invalid: Connection invalid

Running this on macOS 10.13 GM Seed and XCode 9.


Followed along to WWDC 210 -- Core Spotlight for Mac.


When my app starts up the NSCoreDataCoreSpotlightDelegate appears to be registered correctly and returning attributeSets (when I set breakpoints, they look correct).


However, I am getting the following error:


2017-09-18 22:08:38.507968+0100 MyApp[45244:4807622] [index] IndexConnection invalid: Connection invalid
2017-09-18 22:08:38.513584+0100 MyApp[45244:4807622] [error] error: Error indexing (insert/update): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
CoreData: error: Error indexing (insert/update): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
2017-09-18 22:08:39.872491+0100 MyApp[45244:4807622] [error] error: Error indexing (insert/update): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
CoreData: error: Error indexing (insert/update): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
2017-09-18 22:08:39.873548+0100 MyApp[45244:4807617] [error] error: Error indexing (delete): Error Domain=CSIndexErrorDomain Code=-1 "(null)"
CoreData: error: Error indexing (delete): Error Domain=CSIndexErrorDomain Code=-1 "(null)"


I can't find anything on this specific Spotlight error -- anybody got any ideas?


Regards,

/Damien

Replies

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

Same issue.


Here is my code. It's available in iOS9 or later. But break in 10.13.

Error Code = CSIndexErrorDomain Code=-1 "(null)"


if ([CSSearchableIndex isIndexingAvailable]){
  CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeItem];
  attributeSet.providerDataTypeIdentifiers = @[(NSString *)kUTTypeItem];
  attributeSet.title = @"good";
  attributeSet.contentDescription = @"hao dongxi";
  attributeSet.keywords = @[@"goods"];

  CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"dasdasd" domainIdentifier:@"dro" attributeSet:attributeSet];
  CSSearchableItem *item2 = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"dasdasd" domainIdentifier:@"qpro" attributeSet:attributeSet];
  [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item,item2] completionHandler:^(NSError * _Nullable error) {
  if (error) {
  NSLog(@"error :%@",[error description]);
  }
  }];
  }

I had this same issue with a macOS app linking against the 10.14 SDK. Took me a while to figure it out, but linking the app target against CoreSpotlight.framework fixed the issue for me. Would be great if this mechanism was documented more than just being described in one or two WWDC videos...