NSMetadataQuery does not find documents after moving them to iCloud

566 views Asked by At

In my Mac app I have an NSMetadataQuery to watch the app's iCloud directory. When the user decides to enable iCloud from within the app I do the following in this order:

  1. Call [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] to establish access to the iCloud container
  2. Start a NSMetadataQuery to find all files (pattern: "*") in the iCloud container
  3. Now I iterate over all files in the local container and move them to iCloud by using [fileManager setUbiquitous:YES itemAtURL:fileURL destinationURL:targetURL error:&error]

I have two types of documents that I move to iCloud:

  1. The actual documents stored as file packages
  2. For each document I have a .preview file as simple binary file

My problem is that the meta data query only returns my binary preview files, but not my actual documents until I relaunch my app (or open a Finder window of the app's iCloud folder from the Terminal - in this case my app gets another query result with all files).

So is this some weird caching issue? Is this because the documents are file packages and not simple binary files?

I don't see why that should be a problem. If I restart my app the query does return all files correctly.

Could this be a problem with the way my custom document type is exported in Info.plist?

1

There are 1 answers

0
auco On

Could this be a problem with the way my custom document type is exported in Info.plist?

I suppose so: the issue you are describing is most likely because your document format is a file package (I assume written with NSFileWrapper). Remember that the package bit is set when writing the file, so if you have other apps writing to iCloud, that could make the issue much more confusing as it's not about interpreting a file bundle but writing it in a correct manner.

NSMetadataQuery unfortunately ignores folders, so your files will never be reported back unless you have correctly registered your filetype as bundle in the application that writes the file to the iCloud container.

There are a lot of similar and potentially confusing settings in the info.plist, so many things that can go wrong. I experienced an issue when I registered my document filetype correctly on OS X, but failed to do so on iOS: https://stackoverflow.com/a/14993694/388412

The first thing you should do is to go to https://developer.icloud.com/#documents and see if you're looking at files (correct) or folders (problem).

Work on your info.plist document types until you see the documents as files, then it should work.