Xcode NSMetaDataQuery error on device running IOS 17.5 - [ERROR] couldn't fetch remote operation IDs

Xcode 15.4 running on various IOS simulators and hardware devices from IOS 14.5 to 17.5.

Part of my code presents a backup/restore page to the user which uses NSMetaDataQuery to update the GUI for files being uploaded or downloaded in iCloud. On every device I run the code everything works as expected EXCEPT one which is an iPhone 11 running IOS 17.5 (as of yesterday 17.5.1); there I get the following error once I start the query:

[ERROR] couldn't fetch remote operation IDs: NSError: Cocoa 257 "The file couldn’t be opened because you don’t have permission to view it." "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""

Due to this error I am getting no query updates and thus unable to display whether the file needs to upload, download or is synchronised.

I am not initiating any upload or download of the backup file since it is placed in the ubiquitous container and I leave the up/download of the file over to IOS; all I do with the query is monitor the status of the file and take appropriate action to show the user the percentage of up/downloaded file.

As said before it is only the one device causing me headaches so I don't know whether it has anything to do with IOS 17.5 that Apple have made changes that I am unaware of. I have access to an iPhone and an iPad running some version of IOS 16 and it's performing flawlessly. I have no other IOS17+ device to test on.

The code runs very well on any 17.5 simulator, but we all know there are always some differences running code on a Device vs Simulator.

Running 'startAccessingSecurityScopedResource()' which has been suggested by some returns 'true' on the simulator and 'false' on a device, but even then all devices work except one; so that does not seem to be the solution. Changing the query predicate has not helped either.

How do I drill down to find the culprit - I'm at my wits' end.

My very simple query initializer, startup & observers: (Please note, the code shown here is what's left after commenting out everything else. This was done to show that the problem really DOES lie with the Query)

        query = NSMetadataQuery.init()
        query.operationQueue = .main
        query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
        query.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, fileUrl.lastPathComponent)

 query.operationQueue?.addOperation({ [weak self] in
            self?.query.start()
            self?.query.enableUpdates()
        })
    }
    func addNotificationObservers() {
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(queryDidStart(_:)),
            name: .NSMetadataQueryDidStartGathering,
            object: query)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(queryGathering(_:)),
            name: .NSMetadataQueryGatheringProgress,
            object: query)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(queryDidUpdate(_:)),
            name: .NSMetadataQueryDidUpdate,
            object: query)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(queryDidFinishGathering(_:)),
            name: .NSMetadataQueryDidFinishGathering,
            object: query)
    }
Answered by DTS Engineer in 788695022

'startAccessingSecurityScopedResource()' returning false doesn't sound right – If the URL is in your ubiquity container, and the iCloud is configured correctly, the API should return true.

You might try the following steps to reset the device cache and see if that makes any difference:

  1. Remove your app from the device.
  2. Log out iCloud on the device, log back in with the same Apple ID, and then reboot the device. This clears the cached state related to the Apple ID.

'startAccessingSecurityScopedResource()' returning false doesn't sound right – If the URL is in your ubiquity container, and the iCloud is configured correctly, the API should return true.

You might try the following steps to reset the device cache and see if that makes any difference:

  1. Remove your app from the device.
  2. Log out iCloud on the device, log back in with the same Apple ID, and then reboot the device. This clears the cached state related to the Apple ID.
Accepted Answer

You might firstly determine if the issue occurs only on the one single device, or if it actually happens on any iOS 17.5+ devices.

In the former case, the issue is likely that the device configuration has something wrong. You can try to fix it by resetting the device. Or if you'd like to get into the bottom, capture and look into a sysdiagnose to hopefully find a hint from there.

The latter case is definitely worth a closer look. You can check if this Apple sample presents the same issue, and if yes, provide the steps for me to verify. I'd take a closer look from there once I catch a chance.

BTW, the "couldn't fetch remote operation IDs" error is related to file providers (or iCloud Drive). If your files are in your ubiquity containers, as you described, the error doesn't seem relevant to me.

So last night I managed to sneak off with my wife's phone, updated it from IOS 16* to IOS 17.5.1 and 'viola', same problem arose, so then I figured it had to be something with the IOS....or not.

I drilled down on the NSPredicate again and after some changes the following now works: was: NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, fileUrl.lastPathComponent) is now: NSPredicate(format: "%K == %@", NSMetadataItemPathKey, fileUrl.relativePath) --And that now works--

Thank you for the link, it gave me the incentive to tinker with the predicate again.

The above mentioned error is still present on the device, but I can for the foreseeable future ignore it. And the 'startAccessingSecurityScopedResource()' now returns 'true'

Thank you for your assistance.

Xcode NSMetaDataQuery error on device running IOS 17.5 - [ERROR] couldn't fetch remote operation IDs
 
 
Q