Check if UI(Managed)Document is already in iCloud via URLForUbiquityContainerIdentifier+Path instead of NSMetadataQuery?

StackOverflow https://stackoverflow.com/questions/14678782

Question

I have a quite simple shoebox-style iOS app with 1 single Core Data database (as a UIManagedDocument) and thought about trying to add iCloud support.


I of course have to check if there is already an existing database in the cloud *before creating a new UIManagedDocument at startup*, saving/opening it, etc.

As i already know the filename and that there's either 1 document or no document at all, I didn't really get if I had to

  1. start a NSMetaDataQuery with a predicate for the exact filename and then get the fileURL from the result (and download it explicitly?) and open it if there is one, or

  2. just use [[NSFileManager defaultManager] fileExistsAtPath:self.iCloudDBURL] with iCloudDBURL created from URLForUbiquityContainerIdentifier + appending ? Is this URL only a local one and doesn't check the "real" cloud automatically?


I know the use of UIManagedDocument might not be the "right" way for this kind of app, but I thought it'd easier and I could try..

Was it helpful?

Solution

You need to use the NSMetadataQuery approach.

When using iOS on iCloud, documents don't download automatically-- they only download when you ask for them. Using NSFileManager as you suggest would only tell you if the file existed on the local device. But, the file might exist up in the cloud, not downloaded locally yet. If you use NSMetadataQuery you can find out if the document exists anywhere, even if it's in the cloud and not actually downloaded yet. You can find out about the document if it was created on a different device. This also covers the case where the user deletes and reinstalls the app, but doesn't delete cloud data-- you find out if it exists even though it's not downloaded.

Since you're using UIManagedDocument you shouldn't need to make a specific download call-- it will handle that for you when you open it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top