I am trying to wrap my head around NSUserActivity
s and I am not entirely sure on how to use them properly. I have setup my NSUserActivity properly like so:
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.title = "Title"
attributeSet.contentDescription = "Description"
let activity = NSUserActivity(activityType: ActivityType.activity.rawValue)
activity.persistentIdentifier = ActivityIdentifier.activity.rawValue
activity.title = "Title"
activity.requiredUserInfoKeys = ["Key"]
activity.userInfo = ["Key": data]
activity.isEligibleForSearch = true
activity.contentAttributeSet = attributeSet
self.userActivity = activity
self.userActivity!.becomeCurrent()
Now the activity gets indexed via the becomeCurrent()
method. When I click on the activity in Spotlight everything works fine and the activity can be restored using the userInfo
property.
But how do I delete the activity from Spotlight once it has be used (restored)? In this post the user recommends to use either deleteAllSavedUserActivities(completionHandler:)
which works but I can't use since I don't want to delete all activities or deleteSavedUserActivities(withPersistentIdentifiers:completionHandler:)
which does not work. For the first method the documentation says following however to the second method this does not apply:
Deletes all user activities stored by Core Spotlight...
Instead I could index the activities with the Core Spotlight API like so:
let item = CSSearchableItem(uniqueIdentifier: ActivityIdentifier.activity.rawValue, domainIdentifier: "DomainID", attributeSet: attributeSet)
CSSearchableIndex.default().indexSearchableItems([item]) { error in
if error != nil {
print(error!)
} else {
print("successfully indexed item")
}
}
and delete them with the deleteSearchableItems(withIdentifiers:completionHandler:)
method. The problem with that is, I have to set the relatedUniqueIdentifier
of my attributeSet
and then the userInfo
will be empty once I try to restore the activity (regarding post).
So what should I do, should I use both Core Spotlight and NSUserActivity and use CSSearchableItemAttributeSet
to save the data instead of using the userInfo
(why would apple to that?, why would they add the userInfo then?) or should I index my activity without Core Spotlight, but how do I delete the activity from Spotlight in this case?