iOS NSCoreDataCoreSpotlightDelegate,如何为所有项目重新编制索引 [英] iOS NSCoreDataCoreSpotlightDelegate, how to reindex all items

查看:84
本文介绍了iOS NSCoreDataCoreSpotlightDelegate,如何为所有项目重新编制索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了NSCoreDataCoreSpotlightDelegate,但是我错过了默认expirationDate为1个月的部分,现在我3个月前添加的项目没有出现在搜索索引中。

I have implemented NSCoreDataCoreSpotlightDelegate, and I missed out the part where the default expirationDate is 1 month, now items that I have added 3 months ago are not showing up in the search index.

如何让NSCoreDataCoreSpotlightDelegate重新索引所有项目?

How do I get NSCoreDataCoreSpotlightDelegate to reindex all the items?

我以前可以这样称呼:

let container = NSPersistentContainer(name: "MyApp")
let psd = NSPersistentStoreDescription(url:  FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.kiwlm.MyApp")!.appendingPathComponent("MyApp.sqlite"))

let mcdcsd = MyCoreDataCoreSpotlightDelegate(forStoreWith:psd, model: container.managedObjectModel)

container.persistentStoreDescriptions = [psd]

psd.setOption(mcdcsd, forKey:NSCoreDataCoreSpotlightExporter)

// uncomment the following to reindex all items
// mcdcsd.searchableIndex(CSSearchableIndex.default(), reindexAllSearchableItemsWithAcknowledgementHandler: {})

第一次,它将重新索引所有项目,但是如果我重新-在没有注释的情况下再次运行该应用程序,它将不会重新编制索引。

And the first time, it will reindex all the items, but if I re-run the app with the above line uncommented again, it will not reindex.

如果我卸载了该应用程序,请重新安装,然后取消注释该行,那么它将建立索引

If I uninstall the app, install back, then uncomment the above line, then it will index all again for the first time.

如何使它重新为所有索引编制索引?

How do I get it to reindex everything again?

推荐答案

发生这种情况是因为NSCoreDataCoreSpotlightDelegate的实现使用beginIndexBatch和endIndexBatchWithClientState来重新索引AllSearchableItems。因此,它处于保存索引状态。调用reindexAllSearchableItems时,Spotlight将检查状态并仅重新索引新项目。删除应用程序时,您还将删除索引状态。第一次启动时状态为空,NSCoreDataCoreSpotlightDelegate为所有项目建立索引。

It happens because implementation of NSCoreDataCoreSpotlightDelegate use beginIndexBatch and endIndexBatchWithClientState to reindexAllSearchableItems. So it's saving index state. When you invoke reindexAllSearchableItems Spotlight will check the state and reindex only new items. When you delete the app you also delete index state. At first launch state is empty and NSCoreDataCoreSpotlightDelegate index all items.

我认为出于您的目的,最好设置expirationDate而不是为所有项目重新编制索引。您可以在MyCoreDataCoreSpotlightDelegate上通过重写attributeSet来做到这一点:

I think for your purpose much better to set expirationDate instead of reindex all items. You can do it at MyCoreDataCoreSpotlightDelegate by override attributeSet:

override func attributeSet(for object: NSManagedObject) -> CSSearchableItemAttributeSet? {
    guard let attributeSet = super.attributeSet(for: object) else { return nil }
        if object.entity.name == "YourEntityName"  {
            // Setup necessary attributes
            attributeSet.setValue(Date.distantFuture, forKey: "expirationDate")
        }
    return attributeSet
}

要检查可搜索项的到期日期,可以使用CSSearchQuery:

To check expirationDate of searchable items you can use CSSearchQuery:

class QueryExample {
    var query : CSSearchQuery? = nil
    func startQuery(withTitle title : String) {
        var allItems = [CSSearchableItem]()
        // All the items that start with the specified title property using case insensitive comparison
        let queryString = "title == \"*\(title)*\"c"
        let attributes = ["title", "displayName", "keywords",
                      "contentType"]
        self.query = CSSearchQuery(queryString : queryString,
                               attributes : attributes)
        self.query?.foundItemsHandler = { (items : [CSSearchableItem])
        -> Void in
            allItems.append(contentsOf: items)
        }
        self.query?.completionHandler = { (error ) -> Void in
            for item in allItems {
                print("expirationDate:\(item.expirationDate)")
            }
        }
        self.query?.start()
    }
}

像这样调用查询:

   override func viewDidLoad() {
       let query = QueryExample()
       query.startQuery(withTitle: "S")
   }

您应该在控制台上看到expirationDate:

And you should see expirationDate at console:


expirationDate:4001-01-01 00:00:00 +0000

expirationDate:4001-01-01 00:00:00 +0000

这篇关于iOS NSCoreDataCoreSpotlightDelegate,如何为所有项目重新编制索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆