简体   繁体   English

如何从 Spotlight 索引和取消索引 NSUserActivities

[英]How to index and deindex NSUserActivities from Spotlight

I am trying to wrap my head around NSUserActivity s and I am not entirely sure on how to use them properly.我正试图围绕NSUserActivity进行思考,但我并不完全确定如何正确使用它们。 I have setup my NSUserActivity properly like so:我已经正确设置了我的 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()

Now the activity gets indexed via the becomeCurrent() method.现在,活动通过becomeCurrent()方法获得索引。 When I click on the activity in Spotlight everything works fine and the activity can be restored using the userInfo property.当我单击 Spotlight 中的活动时,一切正常,并且可以使用userInfo属性恢复活动。

But how do I delete the activity from Spotlight once it has be used (restored)?但是,一旦使用(恢复)活动,如何从 Spotlight 中删除活动? 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.在这篇文章中,用户建议使用deleteAllSavedUserActivities(completionHandler:)工作但我不能使用,因为我不想删除所有活动或deleteSavedUserActivities(withPersistentIdentifiers:completionHandler:)不起作用。 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...删除 Core Spotlight 存储的所有用户活动...

Instead I could index the activities with the Core Spotlight API like so:相反,我可以使用 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")
    }
}

and delete them with the deleteSearchableItems(withIdentifiers:completionHandler:) method.并使用deleteSearchableItems(withIdentifiers:completionHandler:)方法删除它们。 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 ).问题是,我必须设置我的attributeSet集的relatedUniqueIdentifier ,然后一旦我尝试恢复活动( 关于帖子), userInfo将为空。

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?那么我应该怎么做,我应该同时使用 Core Spotlight 和 NSUserActivity 并使用CSSearchableItemAttributeSet来保存数据而不是使用userInfo (为什么要这样做?,他们为什么要添加 userInfo 呢?)或者我应该索引我的活动而不Core Spotlight,但在这种情况下如何从 Spotlight 中删除活动?

There is just one thing I figured out: In the apple documentation for the domainIdentifier property of the CSSearchableAttributeSet it sounds like you are supposed to use this property to delete the NSUserActivity我只发现了一件事:在CSSearchableAttributeSetdomainIdentifier属性的苹果文档中,听起来您应该使用此属性来删除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().例如,要删除用户活动,您可以在 NSUserActivity object 的 contentAttributeSet 属性上设置此属性,然后在 default() 上调用 deleteSearchableItems(withDomainIdentifiers:completionHandler:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM