How to delete a specific NSUserActivity search ind

2019-07-14 11:02发布

问题:

I use NSUserActivity to index a user activity for searching. I found a solution to delete a specific NSUserActivity, assign a CSSearchableItemAttributeSet with relatedUniqueIdentifier to 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

And delete it use

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

I don't know if it is a right solution or not. Do you have a better solution to delete a specific NSUserActivity search index?

回答1:

As of iOS 12, there are 2 methods to delete NSUserActivity

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

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

The docs note "Deletes all user activities stored by Core Spotlight or donated as Siri shortcuts."

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