In iOS 11, how to make core data searchable in core spotlight

断了今生、忘了曾经 提交于 2019-12-09 18:49:32

问题


As mentioned at WWDC, iOS 11 should have support to index Core Data so that it is searchable via Spotlight. This is the WWDC presentation:

https://developer.apple.com/videos/play/wwdc2017/210/

However, after watching the video and looking at docs, I still have no idea how to setup my core data objects so they are searchable from the phones spotlight search.

I guess the main questions are:

1.) How do you actually set which attributes of a certain object type are searchable.

2.) How do you setup a NSCoreDataCoreSpotlightDelegate and get it to process the data? It looks like NSPersistentStore has a property

var coreSpotlightExporter: NSCoreDataCoreSpotlightDelegate

but it's readyonly :(

I couldn't find anything that explained this stuff clearly. Thanks!


回答1:


You do not set coreSpotlightExporter directly. Instead, set the NSCoreDataCoreSpotlightExporter option with your export delegate:

container.persistentStoreDescriptions.forEach {
    $0.setOption(MyCoreDataCoreSpotlightDelegate(forStoreWith:$0, model: container.managedObjectModel), forKey:NSCoreDataCoreSpotlightExporter)
}

I added this code before calling loadPersistentStores(completionHandler:).

To set attributes and entities, you update the appropriate properties for NSAttributeDescription and NSEntityDescription or in your .xcdatamodeld in the UI.



来源:https://stackoverflow.com/questions/45623020/in-ios-11-how-to-make-core-data-searchable-in-core-spotlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!