New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrating location of the SQLite store #985
Comments
There is no specific method for migration in MR, but you can leverage MR initialization and cleanup, and use the MR provided coordinator and persistent store to to run the migration. Then you can cleanup MR and manually remove the old files. func migrateCoreDataStack() {
let containerUrlForApplicationGroup = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.xxx.yyy")
let appGroupCoreDataStoreUrl = containerUrlForApplicationGroup?.URLByAppendingPathComponent("Database.sqlite")
let oldStoreName = MagicalRecord.defaultStoreName()
let oldStoreUrl = NSPersistentStore.MR_urlForStoreName(oldStoreName)
if NSFileManager.defaultManager().fileExistsAtPath(oldStoreUrl.path!) == true {
MagicalRecord.setupAutoMigratingCoreDataStack()
MagicalRecord.setLoggingLevel(.Warn)
let coordinator = NSPersistentStoreCoordinator.MR_defaultStoreCoordinator()
let oldPersistentStore: NSPersistentStore? = coordinator?.persistentStoreForURL(oldStoreUrl)
// migrate
let sqliteOptions = ["journal_mode": "WAL"]
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true, NSSQLitePragmasOption: sqliteOptions]
var error: NSError?
coordinator.migratePersistentStore(oldPersistentStore!, toURL: appGroupCoreDataStoreUrl!, options: options, withType: NSSQLiteStoreType, error: &error)
if let error = error {
NSLog("Error migrating: \(error.localizedDescription)")
}
MagicalRecord.cleanUp()
// remove old files
var deleteStoreError: NSError?
if let rawURL = oldStoreUrl.absoluteString {
let shmSidecar = NSURL(string: rawURL.stringByAppendingString("-shm"))
let walSidecar = NSURL(string: rawURL.stringByAppendingString("-wal"))
NSFileManager.defaultManager().removeItemAtURL(oldStoreUrl, error: &deleteStoreError)
NSFileManager.defaultManager().removeItemAtURL(shmSidecar!, error: nil)
NSFileManager.defaultManager().removeItemAtURL(walSidecar!, error: nil)
if let error = deleteStoreError {
NSLog("Error removing old Core Data store files: \(error.localizedDescription)")
}
}
}
} |
Wow. Thank you for the thorough answer. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm moving an app to use core data in an embedded framework. I need to move the sqlite store to the containerURL.
Is this something that is built into MR that I don't see? Or do I need to manage that migration myself? Just move the store manually, and then changing the setupCoreData method with a new location?
The text was updated successfully, but these errors were encountered: