Backup core data
I want to backup my core data database. I've read that all I need to do is save the sqlite file. I'm still not sure if that's really all though. Either way, I've been trying to save the sqlite file but I'm not sure how to do that. I think I can get to it using the code below. Just not sure where to go nex开发者_开发知识库t.
GroupAppDelegate *appDelegate= (GroupAppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *storeURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"Group.sqlite"];
NSData *sqliteData = [[NSData alloc] initWithContentsOfURL:storeURL];
- How do I actually save that file?
- Do I even have to make it NSData?
I have it setup so that I connect to Dropbox and now I need to save the core data there.
If you have the NSURL handle to the sqlite file, you don't need to convert it to an NSData or anything. There are various ways you could retrieve the file from the device: a) have it emailed as an attachment. (look at MFMailComposeViewController) b) transfer the file using the DropBox APIs to the DropBox account. Look at the DropBox APIs c) transfer the file using iTunes. There's some way of doing this, but don't have a reference to it at the moment.
Be aware that the accepted answer is a bad idea as of iOS 7 because the sqlite file has other supporting files and does not function on its own!
Use the following method of NSPersistentStoreCoordinator class, rather than file system APIs, to back up and restore the Core Data store:
- (NSPersistentStore *)migratePersistentStore:(NSPersistentStore *)store toURL:(NSURL *)URL options:(NSDictionary *)options withType:(NSString *)storeType error:(NSError **)error
Note that this is the option we recommend.
Via Apple
精彩评论