NSFilePresenter update not firing on iPad - does on Mac with identical code.

I am writing an App with both Mac and iPad versions that saves its state to a file. The user can store the file in their iCloud and open it both on the Mac and iPad simultaneously. If an update is made on either device the other will update their state to match.

This has been achieved using an NSFilePresenter / NSFileCoordinator approach. Both platforms are using identical code.

At present when I make a change on the iPad, a few moments later the Mac updates as expected.

However, this does not happen the other way around. If I update the data on the Mac the iPad never appears to receive an update.

I've tried this on the simulator also. I get similar behaviour, but can get the iPad to update it I use the simulator's Sync With iCloud function.

Any ideas of what I may be doing wrong - as clearly the code works in one direction!

I have worked out the issue. On iOS one needs to set up an NSMetadataQuery in addition to using a NSFilePresenter. Something like:

let metadataQuery = NSMetadataQuery()
metadataQuery.notificationBatchingInterval = 1
metadataQuery.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
metadataQuery.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, "*.txt")
metadataQuery.start()

And then establish a process to listen for changes. For example using Combine:

NotificationCenter.default.publisher(for: .NSMetadataQueryDidUpdate)
   .receive(on: DispatchQueue.main)
   .sink { [weak self] notification in
					guard let self = self else { return }
					self.metadataQuery.stop()
					for resultSets in 0..<self.metadataQuery.resultCount {
						if let resultSet = self.metadataQuery.result(at: resultSets) as? NSMetadataItem {
							/// DO YOUR MAGIC HERE...
						}
					}
					self.metadataQuery.start()
				}
   .store(in: &backgroundProcesses)
NSFilePresenter update not firing on iPad - does on Mac with identical code.
 
 
Q