코어 데이터 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
파일이 이미 있습니까? 대상 파일에 이미 파일이 있는지 확인하고 복사본 전에 삭제하십시오! –