1
私はNSMetadataQueryを使ってiCloudのドライブ内の特定のフォルダの内容を一覧表示しようとしている:NSMetadataQueryを使用してiCloudドライブフォルダの検索を改善するにはどうすればよいですか?
let query = NSMetadataQuery()
func listAllItemsInTheTestFolder() {
guard let container = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.path else {return}
NotificationCenter.default.addObserver(self, selector: #selector(didFinishGathering), name: .NSMetadataQueryDidFinishGathering, object: query)
query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
query.predicate = NSPredicate(format: "%K BEGINSWITH %@", NSMetadataItemPathKey, "\(container)/Documents/Test/")
query.start()
}
// The issue is that it's taking close to a min and a spike in cpu usage when there are more than 10K items in the container.
// Do note: The folder I'm interested in has less than five items.
@objc func didFinishGathering() {
query.stop()
NotificationCenter.default.removeObserver(self, name: .NSMetadataQueryDidFinishGathering, object: query)
let names = query.results.map{($0 as? NSMetadataItem)?.value(forAttribute: NSMetadataItemDisplayNameKey)}
print("Count: ", query.resultCount, "names: ", names) // Expected
}
query.results
は私が期待していすべての項目があります。しかし、アプリケーションのクラウドコンテナ内のアイテムの数が多い(> 10K)場合、それは時間がかかり、CPUの〜100%を1分間に近づけて使用しています。
私はsearchScopes
を設定および/またはsearchItems
を設定することで、単なるテストフォルダに検索範囲を制限しようとした:
query.searchScopes = ["\(container)/Documents/Test/"]
query.searchItems = ["\(container)/Documents/Test/"]
しかし、動作しませんでした。特定のフォルダに検索を制限する方法がある場合は、教えてください。または、別のAPIがある場合は、検索速度を向上させるために使用できますか?
let queue = OperationQueue()
queue.qualityOfService = .background // If you want to speed up the query,
// try to set a higher QoS value
query.queue = queue
:あなたはクエリは、すべてのCPUを取るしたくない場合は、検索はUIをフリーズし、サービスの低品質を設定しないように