I'm trying to craft an NSMetadataQuery to automatically reopen the last UIDocument in an iOS app. But I'm running into 2 problems. The code is very simple:
self.metadataQuery.predicate = NSPredicate(format: "%K like[c] '*.notes'", NSMetadataItemFSNameKey)
self.metadataQuery.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope]
let sort = NSSortDescriptor(key: NSMetadataItemFSContentChangeDateKey, ascending: true)
self.metadataQuery.sortDescriptors = [sort]
First problem is, changing the sort from ascending to descending does nothing. The results are identical either way. But at least that I can work around by manually sorting.
The other problem, though, is that not all applicable documents are being returned by the search, and I have no idea how to troubleshoot this. Documents opened up manually with the document picker are not included in the results. But these should by all means be included in the
NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope.
Also, I can't run it in the simulator, because none of the "ubiquitious" locations show up there, like the shared Documents folder. And if I try to set the searchScopes to an empty array (according to the documentation, "An empty array indicates that there is no limitation on where the query searches."), it never posts any notifications.
EDIT:
For what it's worth, I've filed a bug report - 38400938.