NSUserActivity 和 CoreSpotlight 的使用

Usage of NSUserActivity and CoreSpotlight

我花了很多时间来寻找 NSUserActivity 相对于 CoreSpotlight 的好处,但我实际上找不到任何东西。

实际上,它无法索引我们通过 NSUserActivity 设置的项目。我在下面附上了我的代码片段,它应该按照苹果文档工作,但它不会。

        let personName = "Jon Doe"
        let activity = NSUserActivity(activityType: "com.SearchAPIs.test”)
        activity.userInfo = ["name": "Jon Doe"]
        activity.title = person.name
        let keywords = personName.componentsSeparatedByString(" ")
        activity.keywords = Set(keywords)
        activity.eligibleForSearch = true
        activity.eligibleForPublicIndexing = true
        activity.expirationDate = NSDate().dateByAddingTimeInterval(16666600)
        activity.becomeCurrent()

我想找到

的答案

Why we need to use NSUserActivity in way of search, since it can be possible with CoreSpotlight?

根据 Apple 文档

To guarantee that the activity and its metadata get indexed, you must hold a strong reference to the activity until it gets added to the index. There are two ways to do this: The first way is to assign the activity to a property in the controller object that creates the activity. The second way is to use the userActivity property of the UIResponder object. If you use the second way, you need to set the metadata—such as information in the userInfo property—in the updateUserActivityState: method; otherwise, the metadata you set on the activity will not be persisted.

你 activity 在被索引之前被释放,最好使用 self 保留。

你的第二个问题的答案对我来说仍然是个谜。

也许它像性能一样,索引中没有项目,重要的是 public 索引 CoreSpotlight 不需要 offer.We 必须等待获得问题答案的更多信息。