NSMetadataQuery with NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope does not find files and folders imported into app container

Number:rdar://FB9631965 Date Originated:2021/09/16
Status: Resolved:
Product:Foundation Product Version:
Classification:Incorrect/Unexpected Behavior Reproducible:
(I have also raised this issue on the Developer forums, here: https://developer.apple.com/forums/thread/689468, and raised TSI #779605308)

I’m trying to use NSMetadataQuery on iOS to track changes to folders users have imported from elsewhere but, no matter what I try, I get no results.

Following the [documentation for searching file metadata with NSMetadataQuery](https://developers.apple.com/library/archive/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/QueryingMetadata.html), I’m creating a live query (albeit in Swift) and listening for […]QueryDidFinishGathering and […]QueryDidUpdate. The former fires, with no results, and the latter never fires.

I’ve also tried following the [Synchronizing Documents in the iCloud Environment](https://developer.apple.com/documentation/uikit/documents_data_and_pasteboard/synchronizing_documents_in_the_icloud_environment) example, adding the appropriate Ubiquity keys to my Info.plist and .entitlements file, with no change. The sample application, however, works as described, but doesn't really match my use case.

I’m importing files and folders using [SwiftUI’s View.fileImporter(isPresented:allowedContentTypes:allowsMultipleSelection:onCompletion:)](https://developer.apple.com/documentation/swiftui/view/fileimporter%28ispresented:allowedcontenttypes:allowsmultipleselection:oncompletion:%29), but can’t see how I might security-scope the NSMetadataQuery’s execution (if that’s even a thing?).

My test project is on GitHub (https://github.com/SteveMarshall/ios-imported-directory-monitoring), but the main parts are below…

My query method:

extension NSMetadataQueryUbiquitousExternalDocumentsTestApp {
    func findAccessibleFiles() {

        fileMonitor = Publishers.MergeMany(
            ].map { NotificationCenter.default.publisher(for: $0) }
            .receive(on: DispatchQueue.main)
            .sink { notification in
                defer { query.enableUpdates() }

                foundItems = query.results as! [NSMetadataItem]
                print("Query posted \(notification.name.rawValue) with results: \(query.results)")

        query.searchScopes = [
        query.predicate = NSPredicate(
            format: "%K LIKE %@",
            argumentArray: [NSMetadataItemFSNameKey, "*"]
        query.sortDescriptors = [
            NSSortDescriptor(key: NSMetadataItemFSNameKey, ascending: true)

        if query.start() {
            print("Query started")
        } else {
            print("Query didn't start for some reason")

- Import external files using View.fileImporter(isPresented:allowedContentTypes:allowsMultipleSelection:onCompletion:)
- Configure and start NSMetadataQuery, per the code provided
- Observe that no results are returned


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!