0 Replies
      Latest reply on Oct 16, 2019 12:33 AM by LeoProgramming
      LeoProgramming Level 1 Level 1 (0 points)

        I am trying to wrap my head around `NSUserActivity`s and I am not entirely sure on how to use them properly. I want to add an activity as soon as the user starts one and save it to Spotlight Search so the user can continue later (and remove it when the user does so). I have setup my NSUserActivity properly like so:


            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



        Now the activity gets indexed via the `becomeCurrent()` method. When I click on the activity in Spotlight everything works fine and the activity can be restored using the `userInfo` property.

        But how do I delete the activity from Spotlight once it has be used (restored)? 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. 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...



        Instead I could index the activities with the Core Spotlight API like so:


            let item = CSSearchableItem(uniqueIdentifier: ActivityIdentifier.activity.rawValue, domainIdentifier: "DomainID", attributeSet: attributeSet)

            CSSearchableIndex.default().indexSearchableItems([item]) { error in

                if error != nil {


                } else {

                    print("successfully indexed item")




        and delete them with the `deleteSearchableItems(withIdentifiers:completionHandler:)` method. 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).


        So what should I do, should I use both Core Spotlight and NSUserActivity but where should I save the state then? 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?


        P.S.: I coudln't make any sense out of this post regarding my problem