Duplicate NSUserActivity Displaying in Spotlight

Hello,

I have the following simple code running in the ViewDidLoad for a simple ViewController:

    self.userActivity = [[NSUserActivity alloc]
                        initWithActivityType:@"com.company.activity-type"];


    self.userActivity.title = self.restaurant.name;
    self.userActivity.keywords = [NSSet setWithArray:@[@"LOOK",@"HERE"]];


    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"suge knight", @"killed 2pac", nil];
    self.userActivity.userInfo = dict;

    CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
    attributeSet.title = @"big hitz";
    attributeSet.contentDescription = [NSString stringWithFormat:@"%@\n%@",@"cali",@"rollin'"];
    attributeSet.thumbnailData = [NSData dataWithData:UIImagePNGRepresentation(self.restaurant.imageForSpotlight)];
    attributeSet.identifier = @"asdf";

    self.userActivity.contentAttributeSet = attributeSet;
    self.userActivity.requiredUserInfoKeys = [NSSet setWithArray:self.userActivity.userInfo.allKeys];
    self.userActivity.eligibleForSearch = YES;
    self.userActivity.eligibleForPublicIndexing = YES;


The following code works great and displays the NSUserActivity in spotlight 100% fine. The problem is that if I visit this ViewController multiple times, duplicates of the same NSUserActivity spotlight results come up in spotlight. What can I do to prevent this? I only want one instance of the activity displaying in spotlight.


Thanks for reading.

Answered by matttancock in 37657022

According to https://developer.apple.com/library/prerelease/ios/technotes/tn2416/_index.html Ideally you'd set a relatedUniqueIdentifier on the attributes but if you do that it won't get indexed at all, so I don't think it's possible with the current state of affairs. *I haven't re-tested under beta 5 yet but Apple marked my Bug Report on this as "works as intended".

Accepted Answer

According to https://developer.apple.com/library/prerelease/ios/technotes/tn2416/_index.html Ideally you'd set a relatedUniqueIdentifier on the attributes but if you do that it won't get indexed at all, so I don't think it's possible with the current state of affairs. *I haven't re-tested under beta 5 yet but Apple marked my Bug Report on this as "works as intended".

This is resolved. I got it to work with the identifier like you said.

So you're saying that setting relatedUniqueIdentifier works for you? Did you test that new entries are correctly indexed and available in search results?

I'm wondering because in my tests, as soon as I set relatedUniqueIdentifier, the item isn't indexed. (This is on beta 5.)

Thats not true. I can index the same item twice (ie coming to the same VC twice and having it indexed) but it only shows ones in the results. You just need the same actitivyType "com.company.activity-type". if your activityType changes it will duplicate index


you would only need to set relatedUniqueIdentifier if you have that same item indexed via CSSsearchableItem. Also setting the relatedUniqueIdentifier without having a CSSsearchableItem AHEAD of the NSUserActivity creation, your NSUserActivity will not appear.

Duplicate NSUserActivity Displaying in Spotlight
 
 
Q