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)
}