Usage of NSUserActivity and CoreSpotlight

梦想与她 提交于 2019-12-10 23:06:47

问题


I have spent plenty of time to find benefit of NSUserActivity over CoreSpotlight, whether i couldn't find anything practically.

Actually, it couldn't index our item, which we set through NSUserActivity. I have attached my snippet below, which supposed to work as per apple documentation,however it won't.

        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()

I would like to find answer of

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


回答1:


According to Apple documentation

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.

You activity is deallocated before getting indexed and best to retain using self .

And the answer to your second question remains mystery to me.

Maybe its like the performance ,no of items in indexing, and importantly the public indexing which CoreSpotlight don't offer.We have to wait a little more to get the answer for your question.



来源:https://stackoverflow.com/questions/32134733/usage-of-nsuseractivity-and-corespotlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!