Warning in iCloud integration

本秂侑毒 提交于 2019-12-11 08:12:03

问题


I am trying to integrating iCloud.

Everything works fine but when I try to read a file from iCloud I get a warning Like:

Foundation called mkdir("/var/mobile/Library/Mobile Documents/.ubd/peer-E8A60A8F-FB9D-8721-F47C-hdffgdfg-v23/ftr/(A Document Being Saved By XYZ)"), it didn't return 0, and errno was set to 1.

My Code to fetch Data:

for (NSMetadataItem *item in results)
        {
            NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];
            NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
            MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];

                [doc openWithCompletionHandler:^(BOOL success) {
                    if (success) {
                        NSData *file = [NSData dataWithContentsOfURL:url];

                        NSString *docDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Import/iCloud"];
                        if (![[NSFileManager defaultManager] fileExistsAtPath:docDir])
                            [[NSFileManager defaultManager] createDirectoryAtPath:docDir withIntermediateDirectories:YES attributes:nil error:nil];

                        NSString *pdfFile = [docDir stringByAppendingPathComponent:filename];
                        if(![[NSFileManager defaultManager] fileExistsAtPath:pdfFile])
                            [[NSFileManager defaultManager] createFileAtPath:pdfFile contents:file attributes:nil];

                        NSLog(@"Successfully loaded data from cloud file name %@", filename);                        
                    }
                    else
                    {
                        NSLog(@"Failed to load data");
                    }
                }];

        }
    }

回答1:


It looks like you have a partially written file in iCloud (based on the A Document Being Saved By XYZ in the error), and your meta data query has returned that to you since it also matches the filename. I ran into a similar situation a few weeks ago and solved it by using the exact path to the file, as in:

NSString *filepath = [containerURL.path stringByAppendingPathComponent:@"MyFileName"];

NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemPathKey, filepath];
[query setPredicate:pred];


来源:https://stackoverflow.com/questions/15338371/warning-in-icloud-integration

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