I have an app that uses Core Spotlight
to index the app content. The application also uses Core Data
, and when creating a NSManagedObject
the details of the object are used for the CSSearchableItem
then added to the Spotlight Search Index
.
The thing is I am under the impression that there is no direction reference to the NSManagedObject
and the CSSearchableItem
so when the item is added to the index it just copies the details.
Here is an example of adding an item to the index.
//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.")
}
}
After adding the item to the index all items are searchable via spotlight search. A function in the appDelegate
takes care of actions when selecting index items.
So everything seems fine until I edit or delete the NSManagedObject
within the app, because the Searchable Items Index
does not update the index the items listed in the index are not up to date and still list deleted/old data.
So how can I keep to CSSearchableIndex
items updated when a NSManagedObject
is updated ?