如何删除特定的NSUserActivity搜索索引?

时间:2016-01-07 04:28:42

标签: ios corespotlight nsuseractivity

我使用NSUserActivity索引用户搜索活动。我找到了删除特定NSUserActivity的解决方案,将CSSearchableItemAttributeSetrelatedUniqueIdentifier分配给NSUserActivity

let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
attributeSet.relatedUniqueIdentifier = objectId

let activity = NSUserActivity(activityType: Employee.domainIdentifier)
activity.title = name
activity.userInfo = userActivityUserInfo
activity.keywords = [email, department]
activity.contentAttributeSet = attributeSet

使用

删除它
[[CSSearchableIndex defaultSearchableIndex] 
deleteSearchableItemsWithIdentifiers: objectId completionHandler:^(NSError *deletionError) {  
        if (deletionError) {  
            NSLog(@"Could not delete items from the search index with error %@", deletionError);  
        }  
    }]; 

我不知道这是否是正确的解决方案。您是否有更好的解决方案来删除特定的NSUserActivity搜索索引?

1 个答案:

答案 0 :(得分:0)

从iOS 12开始,有两种方法可以删除NSUserActivity

class func deleteAllSavedUserActivities(completionHandler: () -> Void)

class func deleteSavedUserActivities(withPersistentIdentifiers: [NSUserActivityPersistentIdentifier], completionHandler: () -> Void)

文档注释“删除所有由Core Spotlight存储或捐赠为Siri快捷方式的用户活动。”

https://developer.apple.com/documentation/foundation/nsuseractivity