【问题标题】:Update Spotlight Search Index when working with Core Data?使用 Core Data 时更新 Spotlight 搜索索引?
【发布时间】:2016-03-13 22:40:45
【问题描述】:

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

问题是我的印象是NSManagedObjectCSSearchableItem 没有方向参考,所以当项目添加到索引时它只是复制细节。

这是一个向索引添加项目的示例。

//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.")
      }
    }

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

所以一切似乎都很好,直到我在应用程序中编辑或删除 NSManagedObject,因为 Searchable Items Index 不会更新索引,索引中列出的项目不是最新的并且仍然列出已删除/旧数据。

那么当NSManagedObject 更新时,我如何才能保持CSSearchableIndex 项目的更新?

【问题讨论】:

    标签: ios swift core-data corespotlight


    【解决方案1】:

    如果您希望自己的索引具有相关性,那么保持索引处于最新状态至关重要。每次使用 Core Data 进行此类操作时,都必须从索引中添加/删除项目。在从 Core Data 中删除之前,您应该使用以下方法从索引中删除项目。您还可以在 CoreSpotlight 文档中找到很多有用的信息。

    - deleteSearchableItemsWithIdentifiers:completionHandler:

    【讨论】:

      【解决方案2】:

      你在正确的轨道上,如果你在你的 NSManagedObject 上实现willSave: 方法,它将在对该记录的任何保存/删除操作中被调用。更多内容在这里

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

      要遵循的步骤:

      1. 在核心数据 NSManagedObject 上实现 willSave 方法 子类
      2. 查找映射到核心数据对象的索引对象
      3. 更新/删除该索引

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

      【讨论】:

      • 这对我来说似乎是更中肯的答案
      【解决方案3】:

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

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

      就像这个例子,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-25
        • 1970-01-01
        • 2011-02-20
        • 2016-06-22
        • 1970-01-01
        • 2011-07-16
        • 2011-05-20
        • 1970-01-01
        相关资源
        最近更新 更多