2017-11-08 19 views
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をフリーズし、サービスの低品質を設定しないように

答えて

0

は、別の操作キュー内のジョブを実行します。
関連する問題