Search code examples
iphonecore-datadatabase-backups

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 next.

GroupAppDelegate *appDelegate= (GroupAppDelegate *)[[UIApplication sharedApplication] delegate];

NSURL *storeURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"Group.sqlite"];

NSData *sqliteData = [[NSData alloc] initWithContentsOfURL:storeURL];
  1. How do I actually save that file?
  2. 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.


Solution

  • 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.