为什么 Spotlight 有时不运行我的查询?
我正在使用 Spotlight API(Carbon 和 Cocoa 版本),似乎每隔一段时间就会出现同样的问题:查询从不运行,并且从不触发任何通知。 然而,我想强调的是,大多数时候它确实运行,所以发生了一些奇怪的事情。
我没有编写任何特定的应用程序。 这只是一个 Spotlight 测试工具,因此不会根据用户输入触发查询。 相反,它是在我的控制器内的 applicationDidFinishLaunching:
中配置和执行的。 最初,我尝试在 awakeFromNib
中执行此操作,但在这种情况下,查询从不运行。 (我的理论是 RunLoop 还没有开始,但我不确定。)
这是来自 Carbon 的 applicationDidFinishLaunching:
的代码:
CFStringRef predicate = CFSTR("kMDItemContentTypeTree == 'public.movie'");
_query = MDQueryCreate(NULL, predicate, NULL, NULL);
_query = (MDQueryRef)CFMakeCollectable(_query);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notify:)
name:nil
object:(id)_query];
MDQueryExecute(_query, kMDQueryWantsUpdates);
我凭记忆写了上面的内容,所以它可能包含以前没有的拼写错误原著里没有。 原始代码编译并运行得很好,除了偶尔根本不工作。
是什么赋予了? 也许 applicationDidFinishLaunching:
不是 Spotlight 查询的正确位置。
I'm playing around with the Spotlight API, both the Carbon and Cocoa versions, and I seem to have the same problem crop up every once in a while: The query never runs, and never fires any notifications. However, I want to stress that most of the time it does run, so something strange is going on.
I'm not writing any particular app. This is just a Spotlight test harness, so the query is not fired based on user input. Instead, it's configured and executed in applicationDidFinishLaunching:
inside of my controller. Originally I tried to do this in awakeFromNib
, but in that case the query never ran. (My theory is that the RunLoop has not started yet, but I'm not sure.)
Here's the code from applicationDidFinishLaunching:
for Carbon:
CFStringRef predicate = CFSTR("kMDItemContentTypeTree == 'public.movie'");
_query = MDQueryCreate(NULL, predicate, NULL, NULL);
_query = (MDQueryRef)CFMakeCollectable(_query);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notify:)
name:nil
object:(id)_query];
MDQueryExecute(_query, kMDQueryWantsUpdates);
I wrote the above from memory, so it may contain typos that weren't in the original. The original code compiles and runs just fine, except for occasionally not working at all.
What gives? Perhaps applicationDidFinishLaunching:
is not the right place for a Spotlight query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 applicationDidFinishLaunching 中启动 Spotlight 查询,没有出现任何问题,所以我认为这不是您的问题。
我有几件事要尝试。 不要将 nil 传递给 addObserver:selector:name:object 中的 name,而是尝试传递 NSMetadataQueryDidFinishGatheringNotification 作为 name,并进行第二次调用,传递 NSMetadataQueryDidUpdateNotification 作为 name。 第一个将在查询第一次完成运行时生成结果。 第二个将提供更新。 (但只有当事情发生变化时。)
I start a Spotlight query in applicationDidFinishLaunching and have no issues so I don't think that is your problem.
I have a couple things to try. Rather than passing nil to name in addObserver:selector:name:object try passing NSMetadataQueryDidFinishGatheringNotification for name and make a second call passing NSMetadataQueryDidUpdateNotification for name. The first will generate a result when the query finishes running for the first time. The second will provide updates. (But only when something changes.)