0

我正在像这样索引 CoreData 实体:

func index(object: SearchIndexable) throws -> Bool {
    guard let entry = NSEntityDescription.insertNewObject(forEntityName: "IndexableEntry", into: context) as? IndexableEntry else {
        return false
    }

    entry.key = object.key
    entry.value = object.value

    try context.save()

    let url = entry.objectID.uriRepresentation()

    print("URL: \(url)")

    guard let doc = SKDocumentCreateWithURL(url as CFURL)?.takeRetainedValue() else {
        return false
    }

    let text = object.indexableText.joined(separator: " ")

    return SKIndexAddDocumentWithText(index, doc, text as CFString, true)
}

稍后,我需要枚举每个索引文档

func enumerateDocuments(_ block: (SearchQueryResult) -> Void) {
    guard let iterator = SKIndexDocumentIteratorCreate(index, nil)?.takeRetainedValue() else {
        return
    }

    while let doc = SKIndexDocumentIteratorCopyNext(iterator)?.takeRetainedValue() {
        guard let url = SKDocumentCopyURL(doc)?.takeRetainedValue() else {
            continue
        }

        print("ENUM-URL: \(url)")

    /// ...
}

当我索引 3 个文档,然后刷新索引并尝试枚举时,生成的日志输出是这样的

URL: x-coredata://C12FF498-4FAA-4158-95B4-C9214FD7F85A/IndexableEntry/p1
URL: x-coredata://C12FF498-4FAA-4158-95B4-C9214FD7F85A/IndexableEntry/p2
URL: x-coredata://C12FF498-4FAA-4158-95B4-C9214FD7F85A/IndexableEntry/p3
ENUM-URL: x-coredata://C12FF498-4FAA-4158-95B4-C9214FD7F85A

为什么枚举器会从原始 URL 中剥离实体名称和实例?

4

0 回答 0