无法使用 NSMetadataquery 和 kMDItemUserTags 获得聚光灯搜索用户标签的结果
Not able to get results for spotlight search for user tags with NSMetadataquery and kMDItemUserTags
在尝试向我的控制器添加例程以搜索用户计算机以查找具有指定用户 (Mavericks+) 标签的用户文件后,我束手无策。我正在 运行 进入将开始搜索的块,但没有返回任何结果并且操作队列为零。
代码如下:
class myController: NSWindowController {
//define metadataQuery
var metadataQuery: NSMetadataQuery!
var metadataQueryDidUpdateObserver: AnyObject?
var metadataQueryDidFinishGatheringObserver: AnyObject?
//initialize notificationsCenter
let notificationsCenter = NotificationCenter.default
func doSpotlightSearch(){
//add observers
notificationsCenter.addObserver(self, selector: "initalGatherComplete:", name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
notificationsCenter.addObserver(self, selector: "batchReturned:", name: NSNotification.Name.NSMetadataQueryDidUpdate, object: nil)
metadataQuery = NSMetadataQuery()
metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "kMDItemUserTags == 'myTags.testTag'")
//start search
metadataQuery.start()
//test to see if query is up and running
while metadataQuery.isGathering{
print(metadataQuery.operationQueue?.operationCount)
print ("result count: \(metadataQuery.resultCount)")
sleep(1)
}
func batchReturned(_ sender: NSNotification) {
print("Running batchReturned func")
let resultCounter = metadataQuery.resultCount
print("Number of results:\(resultCounter)")
let notificationsCenter = NotificationCenter.default
notificationsCenter.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
}
func initalGatherComplete(_ sender: NSNotification) {
print("Running initialGatherComplete func")
metadataQuery.stop()
let resultCounter = metadataQuery.resultCount
print("Number of results:\(resultCounter)")
let notificationsCenter = NotificationCenter.default
notificationsCenter.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
}
当我运行上面的代码时,它显示结果队列为零,结果计数为零。即使我去掉 isGathering 循环,batchReturned 和 initialGatherComplete 函数也永远不会被调用。
我怀疑谓词格式可能存在问题,但是当我使用 mdfind 将查询放入终端时,这工作正常。
来自 NSMetadataQuery.start()
方法的文档:This method must be called from the receiver’s operationQueue or on the main thread.
如果您不是从主线程调用 start()
方法,您有责任自己创建 OperationQueue
并从那里开始,例如:
// Declare on class-level
private lazy var queryOperationQueue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1 // restriction for NSMetadataQuery
return queue
}()
...
func doSpotlightSearch() {
metadataQuery = NSMetadataQuery()
metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "kMDItemUserTags == 'myTags.testTag'")
// assign queue to NSMetadataQuery
metadataQuery.operationQueue = queryOperationQueue
// run query from its queue
metadataQuery.operationQueue?.addOperation {
metadataQuery.start()
}
}
希望清楚。
在尝试向我的控制器添加例程以搜索用户计算机以查找具有指定用户 (Mavericks+) 标签的用户文件后,我束手无策。我正在 运行 进入将开始搜索的块,但没有返回任何结果并且操作队列为零。
代码如下:
class myController: NSWindowController {
//define metadataQuery
var metadataQuery: NSMetadataQuery!
var metadataQueryDidUpdateObserver: AnyObject?
var metadataQueryDidFinishGatheringObserver: AnyObject?
//initialize notificationsCenter
let notificationsCenter = NotificationCenter.default
func doSpotlightSearch(){
//add observers
notificationsCenter.addObserver(self, selector: "initalGatherComplete:", name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
notificationsCenter.addObserver(self, selector: "batchReturned:", name: NSNotification.Name.NSMetadataQueryDidUpdate, object: nil)
metadataQuery = NSMetadataQuery()
metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "kMDItemUserTags == 'myTags.testTag'")
//start search
metadataQuery.start()
//test to see if query is up and running
while metadataQuery.isGathering{
print(metadataQuery.operationQueue?.operationCount)
print ("result count: \(metadataQuery.resultCount)")
sleep(1)
}
func batchReturned(_ sender: NSNotification) {
print("Running batchReturned func")
let resultCounter = metadataQuery.resultCount
print("Number of results:\(resultCounter)")
let notificationsCenter = NotificationCenter.default
notificationsCenter.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
}
func initalGatherComplete(_ sender: NSNotification) {
print("Running initialGatherComplete func")
metadataQuery.stop()
let resultCounter = metadataQuery.resultCount
print("Number of results:\(resultCounter)")
let notificationsCenter = NotificationCenter.default
notificationsCenter.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
}
当我运行上面的代码时,它显示结果队列为零,结果计数为零。即使我去掉 isGathering 循环,batchReturned 和 initialGatherComplete 函数也永远不会被调用。
我怀疑谓词格式可能存在问题,但是当我使用 mdfind 将查询放入终端时,这工作正常。
来自 NSMetadataQuery.start()
方法的文档:This method must be called from the receiver’s operationQueue or on the main thread.
如果您不是从主线程调用 start()
方法,您有责任自己创建 OperationQueue
并从那里开始,例如:
// Declare on class-level
private lazy var queryOperationQueue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1 // restriction for NSMetadataQuery
return queue
}()
...
func doSpotlightSearch() {
metadataQuery = NSMetadataQuery()
metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "kMDItemUserTags == 'myTags.testTag'")
// assign queue to NSMetadataQuery
metadataQuery.operationQueue = queryOperationQueue
// run query from its queue
metadataQuery.operationQueue?.addOperation {
metadataQuery.start()
}
}
希望清楚。