【发布时间】:2016-03-13 22:40:45
【问题描述】:
我有一个使用Core Spotlight 来索引应用内容的应用。该应用程序还使用Core Data,并在创建NSManagedObject 时将对象的详细信息用于CSSearchableItem,然后添加到Spotlight Search Index。
问题是我的印象是NSManagedObject 和CSSearchableItem 没有方向参考,所以当项目添加到索引时它只是复制细节。
这是一个向索引添加项目的示例。
//Spotlight Index Search
// Create an attribute set to describe an item.
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)
// Add metadata that supplies details about the item.
attributeSet.title = "\(object.title)"
attributeSet.contentDescription = "\(object.description)"
// Create an item with a unique identifier, a domain identifier, and the attribute set you created earlier.
let item = CSSearchableItem(uniqueIdentifier: "1", domainIdentifier: "ObjectType", attributeSet: attributeSet)
// Add the item to the on-device index.
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in
if error != nil {
print(error?.localizedDescription)
}
else {
print("Item indexed.")
}
}
将项目添加到索引后,所有项目都可以通过聚光灯搜索进行搜索。 appDelegate 中的一个函数负责选择索引项时的操作。
所以一切似乎都很好,直到我在应用程序中编辑或删除 NSManagedObject,因为 Searchable Items Index 不会更新索引,索引中列出的项目不是最新的并且仍然列出已删除/旧数据。
那么当NSManagedObject 更新时,我如何才能保持CSSearchableIndex 项目的更新?
【问题讨论】:
标签: ios swift core-data corespotlight