Answered. You can use copyItemAtURL but *only* after you ensure that all the
items in the ubiquitous container have been downloaded. Seems that the paths
exist in the container in a way that NSFileManager can see, but of course the
files may not actually be there so copyItemAtURL gets an error.
I wrote a method to ensure a subdirectory of the ubiquitous container is fully
downloaded, then put this copy code on the completion handler where it now
works. I can see ways in which I could probably do this better if I went
backwards a bit, but happy enough with this one for now, see how robust it
proves to be.
Tricky stuff this iCloud, the first time through there's an awful lot of
gotchas. Now at least I have a load of sample code I can crib off the next time
and will do a better job of both design and implementation.
On Apr 9, 2012, at 1:15 PM, Roland King wrote:
> I allow my users to turn iCloud on and off. When they turn it off, it allows
> you to either move the contents of iCloud locally, removing it from the
> cloud, or copy it, so any other devices still using the cloud version can
> continue to use it. The move seems to work just fine using NSFileManager's
> setUbiquitous:itemAtURL:error, but I'm failing to get the copy working,
> here's the code ..
>
> (void)copyICloudToSandbox:(id<HIPDatabaseDelegate>)delegate completion:(void
> (^)(void))completion
> {
> [ delegate message:@"copying iCloud to the local database" ];
> NSURL *sandboxDocumentURL = [ self documentsURLUbiquitous:NO ];
> NSURL *iCloudDocumentURL = [ self documentsURLUbiquitous:YES ];
> dispatch_async( dispatch_get_global_queue(
> DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
> NSError *error;
> [ [ NSFileManager defaultManager ]
> copyItemAtURL:iCloudDocumentURL toURL:sandboxDocumentURL error:&error ];
> dispatch_async( dispatch_get_main_queue(), ^{
> [ delegate message:@"copied iCloud to local" ];
> if( completion )
> completion();
> } );
> } );
> }
>
> the sandboxDocumentURL and iCloudDocumentURL are what you'd expect them to
> be. The data in the documents directory is in subdirectories per document and
> the copy starts fine, it creates the first subdirectory, then creates the
> 'StoreContent' subdirectory under that, I can see that from Xcode showing me
> what's on the device. When it comes however to copy the data file, called
> topicStore, it fails with cocoa error 512, printing out the error message
> shows the following
>
> (lldb) po sandboxDocumentURL
> (NSURL *) $4 = 0x0022e350
> file://localhost/var/mobile/Applications/84768B6A-1F63-4EFA-BEF2-2F64EB723085/Documents/
>
> (lldb) po iCloudDocumentURL
> (NSURL *) $5 = 0x0de24710
> file://localhost/private/var/mobile/Library/Mobile%20Documents/LD65ZXD7W6~com~dotteddogsoft~hippocampus/Documents/
>
> (lldb) po error
> (NSError *) $14 = 0x0ee6bd70 Error Domain=NSCocoaErrorDomain Code=512 "The
> operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xee64050
> {NSUserStringVariant=(
> Copy
> ), NSFilePath=/private/var/mobile/Library/Mobile
> Documents/LD65ZXD7W6~com~dotteddogsoft~hippocampus/Documents/2525AED0-415B-49C8-9CFB-2E0361ADDACF.hippocampus/StoreContent/topicStore,
>
> NSDestinationFilePath=/var/mobile/Applications/77AF614B-90EE-4B71-8531-8E61076B0F83/Documents/2525AED0-415B-49C8-9CFB-2E0361ADDACF.hippocampus/StoreContent/topicStore,
> NSUnderlyingError=0xee69da0 "The operation couldn’t be completed. Bad file
> descriptor"}
>
>
> This doesn't tell me very much. Directories work, but the files don't copy.
> Can I just not use copyItemAtURL: to copy a whole tree out of the private
> iCloud store, or is there something I need to do first? I didn't really want
> to write my own recursive copy routine. I did try wrapping the whole thing
> with an NSFileCoordinator block too but that didn't help.
> _______________________________________________
>
> Cocoa-dev mailing list ([email protected])
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/rols%40rols.org
>
> This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]