Jack Humphries
Jack Humphries

Reputation: 13267

iCloud Cocoa Error 512

I'm trying to copy a file from my iCloud container to my apps' cache directory. I keep getting The operation couldn’t be completed. (Cocoa error 512.) in my log, which is a very broad error meaning NSFileWriteUnknownError, or the write failed and iOS doesn't know why.

Here is the code for when the NSMetadataQuery finishes and has the file names in iCloud. I'll briefly describe the code below.

- (void)loadData:(NSMetadataQuery *)query {

    NSArray *cache = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[cacheDirectory stringByAppendingString:@"/issues"] error:NULL];

    for (NSMetadataItem *item in [query results]) {

        NSURL *name = [item valueForAttribute:NSMetadataItemURLKey];
        NSString *nameString = [item valueForAttribute:NSMetadataItemPathKey];

        NSError *error;

        NSURL *cacheURL = [NSURL fileURLWithPath:cacheDirectory];
        NSURL *destination = [cacheURL URLByAppendingPathComponent:[NSString stringWithFormat:@"issues/%@", [[nameString stringByDeletingLastPathComponent] lastPathComponent]] isDirectory:YES];

        [[NSFileManager defaultManager] copyItemAtURL:[name URLByDeletingLastPathComponent] toURL:destination error:&error];

        }

    }

}

This app syncs newspaper issues. In the iCloud documents container, there are folders with the data (one folder could be 5.1.12), and inside each folder is a PDF file with the same name (5.1.12.pdf).

Any idea why this is happening? Thanks for your help.

iCloud: file://localhost/private/var/mobile/Library/Mobile%20Documents/AppBundleID/Documents/5.3.12/

Local: file://localhost/var/mobile/Applications/F91E115B-ECED-403E-AB61-41F7A99EF928/Library/Caches/issues/5.3.12/

Upvotes: 1

Views: 2970

Answers (3)

RelativeGames
RelativeGames

Reputation: 1593

SOLVED !

copyItemAtURL returns an error if the file is already there. Most of the time you'll want to make something like persistent savegames and keep overwritting the file with new changes.

Use something like this to write your file to iCloud

   BYTE *data = NULL;//Put your file data in here
   int FileSize = 0;//replace with your file size
   NSData *myData = [[NSData alloc] initWithBytes:data length:FileSize];
   [myData writeToFile:[FileURL path] atomically:YES];

Upvotes: 2

Thomas Deniau
Thomas Deniau

Reputation: 2573

When iCloud syncs a file to an iOS device, it will first create an empty, dataless file in your app's container to let you know there is a file available. This is what your NSMetadataQuery reports.

If you look at the other keys of your NSMetadataItem, you'll probably notice it says your item is not downloaded yet.

Before being able to copy that file or to open it, you have to ask iCloud to download it. Taking a coordinated read at the item's path (as you should do around any opening or copying operation) will implicitly download the file.

Upvotes: 2

Halen
Halen

Reputation: 456

Step through the code with breakpoints and/or NSLog. It could be that there's a logic error that isn't returning the directory paths that you were intending.

Upvotes: 0

Related Questions