我想備份和檢索核心數據中的sqlite3數據庫。這是爲了保存和恢復用戶保存在應用程序中的數據。計劃是通過電子郵件發送數據庫,然後在接收設備上打開它,以前的所有數據都會奇蹟般地出現,並且都很好。備份和接收核心數據SQlite3數據庫
我已閱讀了許多關於這個主題的帖子和文檔,但把它放在一起是我可以做一些建議的地方。
什麼到目前爲止,我已經做了:
我已成功使用以下
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"db.sqlite"];
NSError *error = nil;
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePath options:0UL error:&error];
if (error != nil) {
DebugLog(@"Failed to read the file: %@", [error localizedDescription]);
} else {
if (fileData == nil) {
DebugLog(@"File data is nil");
}
}
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:@"back up database"];
[mailView addAttachmentData:fileData
mimeType:@"application/octet-stream"
fileName:@"db.sqlite3"];
[mailView setMessageBody:@"Database attached"
isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
進一步研究,以電子郵件發送sqlite3
數據庫使我相信我一定要附加其他日誌文件到本對此? iOS: How can I create a backup copy of my core data base? And how to export/import that copy?
伊夫記錄這些文件作爲db.sqlite db.sqlite-shm db.sqlite-wal
我的問題是 1.我把所有這些db.sqlite db.sqlite-shm db.sqlite-wal
作爲電子郵件附件? 2.接收這些文件並將其複製到應用程序的過程是什麼?
簡單地打開附件不起作用,因爲它只能被某些應用程序讀取。然後我在我的info.plist中創建了一個文檔類型,我的應用程序作爲應用程序出現在操作表中,作爲接收文檔的應用程序,但會導致應用程序崩潰。incomprehensible archive
因此,總之,如何保存和恢復核心數據
感謝您的回覆,聽起來很有意思......我會試試這個,如果有必要的話可以回覆一些更新的代碼。您能否更詳細地解釋一下您通過文檔文件夾中的iTunes Sharing進行備份的建議?謝謝 – JSA986 2014-10-03 09:31:40
@ JSA986對於iTunes共享請看這裏http://www.raywenderlich.com/1948/itunes-tutorial-for-ios-how-to-integrate-itunes-file-sharing-with-your-ios-app – 2014-10-03 14:33:55
謝謝爲鏈接將檢查出來 – JSA986 2014-10-03 15:36:48