Registering as an indexing delegate

An item you add to the Spotlight index today might change over time. Some items you add to Spotlight could even have an expiration date if you've set the endDate property on the item attributes. Therefore, it's important that you register your app as an indexing delegate.

The indexing delegate is used for a single purpose: re-indexing content. If you've added items that expire over time, Spotlight will ask you to index them again to make sure that the expiration date is still correct. The indexing delegate is also called if something goes wrong with the index itself; for example, if all pieces of data are lost or if the search index becomes corrupted.

The indexing delegate has two required methods: searchableIndex(_:reindexAllSearchableItemsWithAcknowledgementHandler:) and searchableIndex(_:reindexSearchableItemsWithIdentifiers:acknowledgementHandler:).

The first method is called if the index has been lost and everything should be indexed. It's up to you to figure out how to index your entire app. In the MustC app, this would probably mean fetching all family members and indexing them. You could use the movies relationship on the family members to loop over all of the movies that are stored in the database and index them accordingly. Alternatively, you could query both entities separately and index them separately.

The second indexing delegate method receives an array of identifiers that need to be indexed and is used to re-index a specific set of items. There are several reasons for spotlight to call this method, for instance, if an item is about to expire.

After you index the required items, you should call the acknowledgementHandler that the delegate method has received. This will make sure that Spotlight understands that you have successfully executed the tasks you needed to perform. To set the indexing delegate, you set the indexDelegate of the search index. For most apps, the searchable index is accessed through CSSearchableIndex.default().

Now that you know everything about adequately implementing Spotlight indexing, let's see how you can step up your indexing game a little bit more by implementing Universal Links.