将CSSearchableItem添加到Core Spotlight(MacOS)时发生未知错误 [英] Unknown Error when adding an CSSearchableItem to Core Spotlight (MacOS)

查看:189
本文介绍了将CSSearchableItem添加到Core Spotlight(MacOS)时发生未知错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近想用Core Spotlight在我的一个项目中增强搜索功能.但是,每当我向SearchIndex添加CSSearchableItem时,都会在完成处理程序中出现以下错误:

I recently wanted to power the search in one of my projects with Core Spotlight. However, whenever I add an CSSearchableItem to the SearchIndex, I get an error in the completion handler with the description:

The operation couldn’t be completed. (CSIndexErrorDomain error -1.)

根据Apple的参考,错误代码-1指的是Unknown Error,这并不是完全有帮助.我将CoreSpotlight和CoreServices框架都添加到了我的应用程序中,但我真的不知道自己做错了什么.

According to Apple's reference, the error code -1 refers to Unknown Error, which isn't exactly helpful. I added both CoreSpotlight and CoreServices frameworks to my app and I really have no idea of what I might have done wrong.

我整理了一个最小的例子:

I put together a minimal example:

import Foundation
import CoreSpotlight

print("Start indexing...")
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = "test element"
attributeSet.contentDescription = "This is a description."
attributeSet.keywords =  ["test1", "test2", "test3"]
let item = CSSearchableItem(uniqueIdentifier: "123455", domainIdentifier: "TestDomain", attributeSet: attributeSet)
var ready = false
CSSearchableIndex.default().indexSearchableItems([item]) { (error) in
    if error == nil {
        print("Success")
    } else {
        print(error?.localizedDescription)
    }
    ready = true
}
//Wait for the block to finish
while (ready == false) {
    sleep(1)
}
print("Finish indexing...")

我设法从WWDC17编译了Apple的Core Spotlight示例项目,实际上它确实可以正常运行.但是,我无法在系统范围内的Spotlight搜索中获取索引的项目.

I managed to compile Apple's example project for Core Spotlight from WWDC17, and it does in fact work without an error. However, I can't obtain the indexed items with the systemwide Spotlight search.

有人知道会发生什么吗?顺便说一下,我正在运行最新的High Sierra版本.

Does anyone have an idea what might be off? By the way, I'm running the latest High Sierra release.

刚刚看到,实际上还有其他人对此问题有疑问.但是,问题尚未得到解决:使用CoreSpotlight时出错

Just saw, that there is in fact someone else with this question. However, the question isn't answered yet: Error while using CoreSpotlight

[Edit2]更新到10.13.2后,行为发生了变化.放在操场上的这段代码现在正在运行;但是,放在我的应用程序中的相同代码仍然会产生错误,尽管它确实包含更多信息.打印错误对象会导致:

After updating to 10.13.2, the behaviour changed. This piece of code put in a playground is working now; however, the very same code put in my app does still produce an error, it does contain more information though. Printing the error object results in:

Error Domain=CSIndexErrorDomain Code=-1003 "(null)" 
UserInfo={NSUnderlyingError=0x60000105f6e0 
{Error Domain=NSCocoaErrorDomain Code=4097 
"Couldn’t communicate with a helper application."}}

对我来说,这显然是框架中的错误,或者您对此有何看法?

As it displays to me, this is clearly a bug in the framework, or what do you think about that?

推荐答案

对于macOS 10.13,SIP和App Sandbox似乎变得更加严格. 经过大量的挖掘工作之后,这就是我所做的,并且也应该为其他阅读本书的人工作:

With macOS 10.13, SIP and the App Sandbox seem to have become a little more strict. After a lot of digging to get this work, here's what I did, and should work for others reading this too:

  1. 在Xcode中,为运行CoreSpotlight代码的目标切换应用程序沙箱"功能.如果这是在框架中,则它必须是托管应用程序的目标.对于新项目,这不是必需的(有关详细信息,请参见此错误报告)
  2. 为将运行CoreSpotlight代码的应用程序/二进制文件启用开发签名.以我为例,它是测试主机.
  1. In Xcode, toggle the "App Sandbox" capability for your target in which your CoreSpotlight code runs. If this is in a framework, it needs to be the hosting application's target. This shouldn't be necessary for new projects (for details, see this bug report).
  2. Enable development signing for the application/binary which will run the CoreSpotlight code. In my case, it was the test host.

这篇关于将CSSearchableItem添加到Core Spotlight(MacOS)时发生未知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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