iCloud Cocoa Error 512

て烟熏妆下的殇ゞ 提交于 2019-12-11 09:25:51

问题


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/

回答1:


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.




回答2:


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];



回答3:


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.



来源:https://stackoverflow.com/questions/11588965/icloud-cocoa-error-512

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!