NSCoreDataCoreSpotlightDelegate crash: "Can't add store"

We are using Core Data with Core Spotlight integration and a small percentage of our users get a crash on app instantiation. It's very bizarre because 99% of the time it works fine and we have never seen this while developing. The NSInvalidArgumentException reads as "Can't add store" although we know that's not true in most cases because the spotlight database gets filled properly from our Core Data values and search generally works great.


The exception backtrace looks like this:


Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x18ff3f758 __exceptionPreprocess
1  libobjc.A.dylib                0x18f147d00 objc_exception_throw
2  CoreData                       0x192a95a30 -[NSCoreDataCoreSpotlightDelegate initForStoreWithDescription:model:]
3  OhioStateKit                   0x1007da228 @objc CoreSpotlightDelegate.init(forStoreWith:model:) (CoreSpotlightDelegate.swift)
4  OhioStateKit                   0x1007da18c CoreSpotlightDelegate.__allocating_init(forStoreWith:model:) (CoreSpotlightDelegate.swift)
5  OhioStateKit                   0x1007115cc Persistence.constructAndConfigurePersistentContainer(completion:) (Persistence.swift:61)
6  OhioStateKit                   0x100bec434 AppDelegate.application(_:didFinishLaunchingWithOptions:) (AppDelegate.swift:107)
7  OhioStateKit                   0x100bed4c0 @objc AppDelegate.application(_:didFinishLaunchingWithOptions:) (<compiler-generated>)
8  UIKitCore                      0x1bce170c8 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:]
9  UIKitCore                      0x1bce18890 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:]
10 UIKitCore                      0x1bce1e2b0 -[UIApplication _runWithMainScene:transitionContext:completion:]
11 UIKitCore                      0x1bc6b1e20 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke
12 UIKitCore                      0x1bc6ba7e8 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:]
13 UIKitCore                      0x1bc6b1a88 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]
14 UIKitCore                      0x1bc6b23a4 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:]
15 UIKitCore                      0x1bc6b05ec __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke
16 UIKitCore                      0x1bc6b02b0 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]
17 UIKitCore                      0x1bc6b4fd0 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke
18 UIKitCore                      0x1bc6b5e1c _performActionsWithDelayForTransitionContext
19 UIKitCore                      0x1bc6b4e84 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]
20 UIKitCore                      0x1bc6b9e68 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:]
21 UIKitCore                      0x1bce1c7ec -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]
22 UIKitCore                      0x1bc9ff648 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]
23 FrontBoardServices             0x1928fd3a8 -[FBSSceneImpl _didCreateWithTransitionContext:completion:]
24 FrontBoardServices             0x1929076e4 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2
25 FrontBoardServices             0x192906e34 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke
26 libdispatch.dylib              0x18f9518f4 _dispatch_client_callout
27 libdispatch.dylib              0x18f954ecc _dispatch_block_invoke_direct
28 FrontBoardServices             0x19293a218 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
29 FrontBoardServices             0x192939e94 -[FBSSerialQueue _performNext]
30 FrontBoardServices             0x19293a490 -[FBSSerialQueue _performNextFromRunLoopSource]
31 CoreFoundation                 0x18fecf954 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
32 CoreFoundation                 0x18fecf8d0 __CFRunLoopDoSource0
33 CoreFoundation                 0x18fecf18c __CFRunLoopDoSources0
34 CoreFoundation                 0x18fec9e60 __CFRunLoopRun
35 CoreFoundation                 0x18fec9764 CFRunLoopRunSpecific
36 GraphicsServices               0x192105dd4 GSEventRunModal
37 UIKitCore                      0x1bce2000c UIApplicationMain
38 Ohio State                     0x1004e78c0 main (main.swift:29)
39 libdyld.dylib                  0x18f988ffc start


The instantiation code mostly looks like this:


let persistentStoreUrl = Device.shared.applicationSupportDirectory().appendingPathComponent("OSU Mobile.sqlite")
let storeDescription = NSPersistentStoreDescription(url: persistentStoreUrl)
let container = NSPersistentContainer(name: "OSU Mobile", managedObjectModel: model)
storeDescription.setOption(CoreSpotlightDelegate(forStoreWith: storeDescription, model: container.managedObjectModel): NSCoreDataCoreSpotlightExporter)
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
    // The app is ready to go
}


Has anyone experienced anything like this? From what we can tell from the somewhat sparse documentation we think we are using it correctly.

Replies

I know this thread is quite old but I wanted to call your attention to the fact the NSCoreDataCoreSpotlightDelegate object has been updated with new features and functionality! In our session, "Showcase App Data in Spotlight", you'll:

  • Discover how Core Data can surface data from your app in Spotlight with as little as two lines of code.
  • Learn how to make that data discoverable in Spotlight search and to customize how it is presented to people on device.
  • See how to implement full-text search within your app, driven completely with the data indexed by Spotlight.

We've released a [sample application](Showcase App Data in Spotlight) and updated our documentation to demonstrate how these new capabilities work.

For more information be sure to watch our session Showcase App Data in Spotlight on Wednesday.