使用 NSUserActivity 或 Core Spotlight

Use NSUserActivity or Core Spotlight

我阅读了一些教程,发现当用户在应用程序中对一组内容数据进行索引时,NSUserActivity 用于索引信息,activity,Core Spotlight 用于索引信息。

使用 NSUserActivity

var activity = NSUserActivity(activityType: "com.example.demo.searchapi")
activity.title = contactEntity.name
activity.userInfo = ["id": contactEntity.uid]
activity.eligibleForSearch = true
activity.keywords = NSSet(array: [contactEntity.name,contactEntity.phoneNumber, contactEntity.email]) as! Set

activity.becomeCurrent()

使用 Core Spotlight

let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
attributeSet.title = contactEntity.name
attributeSet.relatedUniqueIdentifier = contactEntity.uid

let searchableItem = CSSearchableItem(uniqueIdentifier: contactEntity.uid, domainIdentifier: "com.example.demo.searchapi", attributeSet: attributeSet)

CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { error in  
if let error = error { 
    print("Error indexing: \(error)")
} else {
    print("Indexed.")
}

但我认为两个解决方案结果之间没有区别。如果用户使用 activity,我可以使用 Core Spotlight,而不是 NSUserActivity。它给了我同样的结果。

那么,为什么 Apple 必须提供两种结果相同的不同解决方案?

我现在将 Core Spotlight 和 NSUserActivity 包括到我的应用程序中,我开始了解一些差异。

正如@PGDev 在您的评论中所述,NSUserActivity 可以设置为公开索引(在 Apple 的服务器上),这有助于显示在其他用户的搜索结果中。而 Core Spotlight 仅适用于每个设备。

与适用于我的应用程序的 NSUserActivity 最大的有益区别在于,执行相同 activity 的用户越多,比如阅读相同的新闻报道,索引变得越受欢迎,并且可以产生有益的结果。 Apple - Enhance Your Search Results

  • The frequency with which users view your content (which is captured when you use NSUserActivity)
  • The amount of engagement users have with your content (determined by the engagement ratio, which is based on the number of times users tap an item related to your app and the number of app-related items that are displayed in search results)
  • The popularity of a URL in your website and the amount of structured data available