Safely combining indexing methods

Since no unique information is associated with the user activities yet, Spotlight can't figure out that a family member that's indexed through a user activity is the same family member that was already inserted as a searchable item. To ensure that Spotlight understands this, you should add two more factory methods that create an activity item for either a family member or a movie with the correct information associated with them. Add the following methods to IndexingFactory:

static func activity(forMovie movie: Movie) -> NSUserActivity {
  let activityItem = activity(withType: .movieDetailView, name: movie.title!, makePublic: false)
  let attributes = searchableAttributes(forMovie: movie)
  attributes.domainIdentifier = DomainIdentifier.movie.rawValue
  activityItem.contentAttributeSet = attributes

  return activityItem
}

static func activity(forFamilyMember familyMember: FamilyMember) -> NSUserActivity {
  let activityItem = activity(withType: .movieDetailView, name: familyMember.name!, makePublic: false)
  let attributes = searchableAttributes(forFamilyMember: familyMember)
  attributes.domainIdentifier = DomainIdentifier.familyMember.rawValue
  activityItem.contentAttributeSet = attributes

  return activityItem
}

The most important lines to take note of are the ones where a domainIdentifier is set on the attributes constant. Since iOS 10, developers have been able to associate a domainIdentifier with user activities through contentAttributeSet. By adding a domainIdentifier to the indexed item, searchable items and user activities are unified even more. Update the viewDidAppear implementation for MovieDetailViewController as follows:

override func viewDidAppear(_ animated: Bool) {  
    super.viewDidAppear(animated)  

    guard let movie = self.movie  
        else { return }  

    self.userActivity = IndexingFactory.activity(forMovie: movie)  
    self.userActivity?.becomeCurrent()  
}

We also need to update the viewDidAppear method in MoviesViewController. You should be able to do this on your own; the code will look similar to the preceding snippet, except you're indexing a family member instead of a movie.

Now that all of your app contents are indexed in Spotlight, it's time to discuss some of the methods Spotlight uses to rate your content and the best practices you should keep in mind when you add your app's contents to Spotlight.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246