我试着去创造核心数据UIManagedDocument备份,并将它们存储在iCloud中。它已经> 2个月,因为我试图做到这一点,我不明白如何做到这一点。有在互联网上没有任何信息在所有... 我尝试至少创建本地备份和它不工作,要么 这是代码:UImanagedDocument的persistentStore iCloud云备份
-(void)testCopyStoreToDocuments
{
AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.userDataDocument closeWithCompletionHandler:^(BOOL closed)
{
if(closed)
{
@autoreleasepool {
NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSString *sourceFile = [[[[LoadingManager localDocumentURL]URLByAppendingPathComponent:@"StoreContent"]path] stringByAppendingPathComponent:@"persistentStore"];
NSURL *sourceURL = [NSURL fileURLWithPath:sourceFile];
[fc coordinateReadingItemAtURL:sourceURL options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL *sourceURLtoUse) {
NSError *error = nil;
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *destinationFile = [[[LoadingManager localDocumentsDirectoryURL]path] stringByAppendingPathComponent:@"persistentStore"];
//simply copy the file over
BOOL copySuccess = [fm copyItemAtPath:[sourceURLtoUse path]
toPath:destinationFile
error:&error];
if (copySuccess) {
NSLog(@" copied file successfully");
} else {
NSLog(@"Error copying item at path: %@\nTo path: %@\nError: %@", sourceFile, destinationFile, error);
}
}];
fc = nil;
}
}
}];
}
-(void)testReplaceStore
{
AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.userDataDocument closeWithCompletionHandler:^(BOOL closed)
{
if(closed)
{
NSFileCoordinator *fc = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fc coordinateWritingItemAtURL:[[[LoadingManager localDocumentURL]URLByAppendingPathComponent:@"StoreContent"]URLByAppendingPathComponent:@"persistentStore"] options:NSFileCoordinatorWritingForDeleting error:nil byAccessor:^(NSURL *sourceURLtoUse){
NSLog(@"%@",sourceURLtoUse);
NSError * error = nil;
NSLog(@"replacment: %hhd",[[NSFileManager defaultManager]replaceItemAtURL:sourceURLtoUse withItemAtURL:[[LoadingManager localDocumentsDirectoryURL]URLByAppendingPathComponent:@"persistentStore"] backupItemName:@"backUp" options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:nil error:&error]);
NSLog(@"%@",error);
}];
NSLog(@"stores: %@",appDelegate.userDataDocument.managedObjectContext.persistentStoreCoordinator.persistentStores);
[appDelegate.userDataDocument saveToURL:appDelegate.userDataDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL saved)
{
if(saved)
{
[appDelegate.userDataDocument openWithCompletionHandler:^(BOOL opened)
{
if(opened)
{
NSLog(@"opened");
}
}];
}
else
{
NSLog(@"failed to save");
NSLog(@"stores: %@",appDelegate.userDataDocument.managedObjectContext.persistentStoreCoordinator.persistentStores);
}
}];
}
}];
}
它Nslogs错误时更换正所谓:
replacment: 0
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x15d91540 {NSFileNewItemLocationKey=file:///var/mobile/Applications/85974C93-75FD-406A-B1BF-EDE7DFC25FE2/Documents/persistentStore, NSFileOriginalItemLocationKey=file:///var/mobile/Applications/85974C93-75FD-406A-B1BF-EDE7DFC25FE2/Documents/Data%20Document/StoreContent/persistentStore, NSUnderlyingError=0x15db0080 "The operation couldn’t be completed. (Cocoa error 260.)",
这将有助于,如果你描述了你遇到的问题与上面的代码。 –
@TomHarrington ive已更新问题 – t0a0
该文件是否已经存在?检查目标文件是否已经存在,并在复制之前删除它!顺便说一句 –