2

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?

huynguyen
  • 7,616
  • 5
  • 35
  • 48
  • 1
    U can try `deleteSearchableItemsWithIdentifiers` or `deleteSearchableItemsWithDomainIdentifiers` then readd? – Tj3n Jan 07 '16 at 04:35
  • @Tj3n Oh, that is my current solution, one vote up for you. Sorry because I miss delete method in code. I will edit above code. My question is, it has other solutions to delete use NSUserActivity method, instead of linking NSUserActivity to a CSSearchableItemAttributeSet and delete use Core Spotlight medthod? – huynguyen Jan 07 '16 at 04:48
  • 1
    From my knowledge then no :/ I always have to use those code when i want to delete some searchable item – Tj3n Jan 07 '16 at 05:11

1 Answers1

0

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

Casey Wagner
  • 1,528
  • 1
  • 11
  • 12