使用Core Data时更新Spotlight搜索索引?

时间:2022-09-10 19:18:30

I have an app that uses Core Spotlight to index the app content. The application also uses Core Data, and when creating a NSManagedObject the details of the object are used for the CSSearchableItem then added to the Spotlight Search Index.

我有一个使用Core Spotlight索引应用内容的应用。该应用程序还使用Core Data,在创建NSManagedObject时,对象的详细信息将用于CSSearchableItem,然后添加到Spotlight搜索索引中。

The thing is I am under the impression that there is no direction reference to the NSManagedObject and the CSSearchableItem so when the item is added to the index it just copies the details.

事情是我的印象是没有NSManagedObject和CSSearchableItem的方向引用,所以当项目被添加到索引时,它只是复制细节。

Here is an example of adding an item to the index.

以下是将项添加到索引的示例。

//Spotlight Index Search
// Create an attribute set to describe an item.

    let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String)

// Add metadata that supplies details about the item.

    attributeSet.title = "\(object.title)"
    attributeSet.contentDescription = "\(object.description)"

// Create an item with a unique identifier, a domain identifier, and the attribute set you created earlier.
    let item = CSSearchableItem(uniqueIdentifier: "1", domainIdentifier: "ObjectType", attributeSet: attributeSet)

// Add the item to the on-device index.
       CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in

    if error != nil {
      print(error?.localizedDescription)
    }
      else {
      print("Item indexed.")
      }
    }

After adding the item to the index all items are searchable via spotlight search. A function in the appDelegate takes care of actions when selecting index items.

将项目添加到索引后,所有项目都可通过聚光灯搜索进行搜索。 appDelegate中的函数在选择索引项时负责处理操作。

So everything seems fine until I edit or delete the NSManagedObject within the app, because the Searchable Items Index does not update the index the items listed in the index are not up to date and still list deleted/old data.

因此,在我编辑或删除应用程序内的NSManagedObject之前,一切似乎都很好,因为可搜索项目索引不更新索引,索引中列出的项目不是最新的,仍然列出已删除/旧数据。

So how can I keep to CSSearchableIndex items updated when a NSManagedObject is updated ?

那么当更新NSManagedObject时,如何更新CSSearchableIndex项?

3 个解决方案

#1


1  

You want the indexing to be in sync with the deletion of your items. Hence look for these three methods implemented by CSSearchableIndex class to delete items when they are no longer required.

您希望索引与删除项目同步。因此,请查看CSSearchableIndex类实现的这三种方法,以便在不再需要时删除项目。

  1. deleteAllSearchableItemsWithCompletionHandler(_:)
  2. deleteAllSearchableItemsWithCompletionHandler(_ :)
  3. deleteSearchableItemsWithDomainIdentifiers(_:completionHandler:)
  4. deleteSearchableItemsWithDomainIdentifiers(_:completionHandler :)
  5. deleteSearchableItemsWithIdentifiers(_:completionHandler:)
  6. deleteSearchableItemsWithIdentifiers(_:completionHandler :)

Like this example,

像这个例子,

CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithDomainIdentifiers(["tv-shows"]) { (error) -> Void in
    if error != nil {
        print(error?.localizedDescription)
    }
    else {
        // Items were deleted successfully
    }
}

#2


4  

Keeping index up to date is a critical if you want your index relevant. You have to add/remove items from index each time when you doing such operation with Core Data. You should use the following method to remove items from index before deleting from Core Data. Also you can find lots of useful info in CoreSpotlight docs.

如果您希望索引相关,则保持索引最新是至关重要的。每次使用Core Data执行此类操作时,都必须在索引中添加/删除项目。在从Core Data中删除之前,应使用以下方法从索引中删除项目。您还可以在CoreSpotlight文档中找到许多有用的信息。

- deleteSearchableItemsWithIdentifiers:completionHandler:

- deleteSearchableItemsWithIdentifiers:completionHandler:

#3


2  

You are on the right track, if you implement the willSave: method on your NSManagedObject, it will be invoked on any save/delete operation against that record. more on that here

您处于正确的轨道上,如果在NSManagedObject上实现了willSave:方法,则将针对该记录对任何保存/删除操作调用它。更多关于这一点

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html#//apple_ref/occ/instm/NSManagedObject/willSave

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html#//apple_ref/occ/instm/NSManagedObject/willSave

Steps To Follow:

要遵循的步骤:

  1. Implement willSave method on your core data NSManagedObject Subclasses
  2. 在核心数据NSManagedObject子类上实现willSave方法
  3. Lookup your indexed object that maps to the core data object
  4. 查找映射到核心数据对象的索引对象
  5. Update/Delete that index
  6. 更新/删除该索引

Tip: If you serialize your NSManagedObjectID for the core data object, (assuming you obtained the permanent ID for the object prior to saving it), you can use that to serve as the unique Identifier for your search index item so you can quickly find/update it

提示:如果为核心数据对象序列化NSManagedObjectID(假设您在保存对象之前获得了对象的永久ID),则可以将其用作搜索索引项的唯一标识符,以便快速查找/更新它

#1


1  

You want the indexing to be in sync with the deletion of your items. Hence look for these three methods implemented by CSSearchableIndex class to delete items when they are no longer required.

您希望索引与删除项目同步。因此,请查看CSSearchableIndex类实现的这三种方法,以便在不再需要时删除项目。

  1. deleteAllSearchableItemsWithCompletionHandler(_:)
  2. deleteAllSearchableItemsWithCompletionHandler(_ :)
  3. deleteSearchableItemsWithDomainIdentifiers(_:completionHandler:)
  4. deleteSearchableItemsWithDomainIdentifiers(_:completionHandler :)
  5. deleteSearchableItemsWithIdentifiers(_:completionHandler:)
  6. deleteSearchableItemsWithIdentifiers(_:completionHandler :)

Like this example,

像这个例子,

CSSearchableIndex.defaultSearchableIndex().deleteSearchableItemsWithDomainIdentifiers(["tv-shows"]) { (error) -> Void in
    if error != nil {
        print(error?.localizedDescription)
    }
    else {
        // Items were deleted successfully
    }
}

#2


4  

Keeping index up to date is a critical if you want your index relevant. You have to add/remove items from index each time when you doing such operation with Core Data. You should use the following method to remove items from index before deleting from Core Data. Also you can find lots of useful info in CoreSpotlight docs.

如果您希望索引相关,则保持索引最新是至关重要的。每次使用Core Data执行此类操作时,都必须在索引中添加/删除项目。在从Core Data中删除之前,应使用以下方法从索引中删除项目。您还可以在CoreSpotlight文档中找到许多有用的信息。

- deleteSearchableItemsWithIdentifiers:completionHandler:

- deleteSearchableItemsWithIdentifiers:completionHandler:

#3


2  

You are on the right track, if you implement the willSave: method on your NSManagedObject, it will be invoked on any save/delete operation against that record. more on that here

您处于正确的轨道上,如果在NSManagedObject上实现了willSave:方法,则将针对该记录对任何保存/删除操作调用它。更多关于这一点

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html#//apple_ref/occ/instm/NSManagedObject/willSave

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html#//apple_ref/occ/instm/NSManagedObject/willSave

Steps To Follow:

要遵循的步骤:

  1. Implement willSave method on your core data NSManagedObject Subclasses
  2. 在核心数据NSManagedObject子类上实现willSave方法
  3. Lookup your indexed object that maps to the core data object
  4. 查找映射到核心数据对象的索引对象
  5. Update/Delete that index
  6. 更新/删除该索引

Tip: If you serialize your NSManagedObjectID for the core data object, (assuming you obtained the permanent ID for the object prior to saving it), you can use that to serve as the unique Identifier for your search index item so you can quickly find/update it

提示:如果为核心数据对象序列化NSManagedObjectID(假设您在保存对象之前获得了对象的永久ID),则可以将其用作搜索索引项的唯一标识符,以便快速查找/更新它