我正在努力思考 NSUserActivity
,但我并不完全确定如何正确使用它们。我已经像这样正确设置了我的 NSUserActivity:
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()
现在事件通过 becomeCurrent()
方法获得索引。当我单击 Spotlight 中的事件时,一切正常,并且可以使用 userInfo
属性恢复事件。
但是,如何在使用(恢复)后从 Spotlight 中删除该事件?在这个post用户建议使用 deleteAllSavedUserActivities(completionHandler:)
这行得通,但我不能使用,因为我不想删除所有事件或 deleteSavedUserActivities(withPersistentIdentifiers:completionHandler:)
这不起作用。对于 first method文档说要遵循 second method这不适用:
Deletes all user activities stored by Core Spotlight...
相反,我可以像这样使用 Core Spotlight API 为事件编制索引:
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")
}
}
并使用 deleteSearchableItems(withIdentifiers:completionHandler:)
方法删除它们。问题是,我必须设置 attributeSet
的 relatedUniqueIdentifier
然后 userInfo
将在我尝试恢复事件后为空(regarding post)。
那么我应该怎么做,我应该同时使用 Core Spotlight 和 NSUserActivity 并使用 CSSearchableItemAttributeSet
来保存数据而不是使用 userInfo
(为什么要苹果? ,那么他们为什么要添加 userInfo?)或者我应该在没有 Core Spotlight 的情况下为我的事件编制索引,但在这种情况下如何从 Spotlight 中删除事件?
最佳答案
只有一件事我想通了:在 apple documentation 中对于 CSSearchableAttributeSet
的 domainIdentifier
属性,听起来您应该使用此属性删除 NSUserActivity
Specify a domain identifier to group items together and to make it easy to delete groups of items from the index. For example, to delete a user activity, you can set this property on the contentAttributeSet property of the NSUserActivity object and then call deleteSearchableItems(withDomainIdentifiers:completionHandler:) on the default().
关于swift - 如何从 Spotlight 索引和取消索引 NSUserActivities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373782/