How to convert a Persistent Store from XML to SQLite in Core Data

By default Mac Apps use XML as store type when saving in Core Data, even though it can use other formats – such as SQLite, which is the default on iOS. Here’s a way to quickly convert one into the other.

Note that this is designed as a one-off operation to create a new store file which can be used in another application. Once run, comment the code out and either change your store file, or take the store file and use it in another app.

Add this code to the AppDelegate.m file in the persistenStore method (just before the return statement):

// convert this thing to SQL
NSURL *sqlstore = [applicationFilesDirectory URLByAppendingPathComponent:@"NewStoreFile.sqldata"];
NSPersistentStore *xmlstore = [coordinator persistentStoreForURL:url];
    
[coordinator migratePersistentStore:xmlstore toURL:sqlstore options:nil withType:NSSQLiteStoreType error:nil];

For more information on how to do this in context, check out Simon Allardice’s superb course Core Data for iOS and OS X on Lynda.com.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.