Skip to content

Instantly share code, notes, and snippets.

@ZachOrr
Last active December 14, 2020 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZachOrr/396bbb443d428a3174c4e102c344589a to your computer and use it in GitHub Desktop.
Save ZachOrr/396bbb443d428a3174c4e102c344589a to your computer and use it in GitHub Desktop.
Notes about the undocumented changes to NSCoreDataCoreSpotlightDelegate I've seen in iOS 14

There appears to be some undocumented changes to the NSCoreDataCoreSpotlightDelegate in iOS 14. I've been unable to find these changes documented in the NSCoreDataCoreSpotlightDelegate documentation or anywhere else online.

Debug Logging

There's quite a bit of debug logging around NSCoreDataCoreSpotlightDelegate now, including initialization and indexing requets.

Enable NSPersistentHistoryTrackingKey

NSCoreDataCoreSpotlightDelegate requires NSPersistentHistoryTrackingKey to be enabled in NSPersistentStoreDescription options. (NSInternalInconsistencyException)

The fix is fairly easy - set the NSPersistentHistoryTrackingKey on your NSPersistentStoreDescription

persistentContainer.persistentStoreDescriptions.forEach {
  $0.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
}

Migrate init() to init(forStoreWith:model:)

'init()' is unavailable

Fairly straightforward fix - init() used to be available but no longer is. Use the new init(forStoreWith:model:) instead.

let indexDelegate: NSCoreDataCoreSpotlightDelegate = {
  let description = persistentContainer.persistentStoreDescriptions.first!
  return NSCoreDataCoreSpotlightDelegate(forStoreWith: description,
                                                model: persistentContainer.managedObjectModel)
}()

Migrate any NSInMemoryStoreType to NSSQLiteStoreType

NSCoreDataCoreSpotlightDelegate requires the store type to be NSSQLiteStoreType.

Also a fairly straightforward fix, assuming your stack supports the change. NSCoreDataCoreSpotlightDelegate no longer supports a NSPersistentStoreDescription with the NSInMemoryStoreType or NSBinaryStoreType types - it must be NSSQLiteStoreType.

let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
persistentContainer.persistentStoreDescriptions = [description]

let spotlightDelegate = NSCoreDataCoreSpotlightDelegate(forStoreWith: description,
                                                               model: persistentContainer.managedObjectModel)

Indexing Requests Debug Logging

CoreData: debug: Dropping "duplicate" indexing request (2).
CoreData: debug: Need additional indexing operation (11)
CoreData: debug: Do *not* need additional indexing operation (0).

WIP - I've yet to figure out these logs. If anyone has any leads, leave a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment