I've been trying to grab the NSMetaDataItems for a collection of items in a known file location outside the main App Container (I want to manage the documents in place rather than copying them into the app container to manage). I think I've worked out the code to start the query, but the NSMetadataQueryDidFinishGathering notification is never being called. I've tried changing the predicate comparator from '==' to '==[cd]' to 'LIKE' to 'BEGINSWITH' and nothing works. I also found a resource that said it was because it's not being called on the main queue, so I added code for that. But it's still not being called. I've been stumped on this for months. Any help would be appreciated.
NotificationCenter.default.addObserver(self, selector: Selector(("initialGathercomplete:")), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object:nil)
metadataQuery.notificationBatchingInterval = 1
metadataQuery.searchScopes = [NSMetadataQueryUbiquitousDataScope]
metadataQuery.predicate = NSPredicate(format: "%K ==[cd] %@", NSMetadataItemPathKey, libraryPath)
metadataQuery.sortDescriptors = [NSSortDescriptor(key: NSMetadataItemFSNameKey, ascending: true)]
DispatchQueue.main.async{
self.metadataQuery.start()
}