If you're doing this to solve the "data model is not the same` crash, you can delete the file and create a new one in your persistent store coordinator like so:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Data.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
println("Unresolved error \(error), \(error!.userInfo)")
if NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
if NSFileManager.defaultManager().removeItemAtPath(url.path!, error: nil) {
var newCoordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
if newCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) != nil {
return newCoordinator
}
}
}
}
return coordinator
}()
I don't do much in objective-C anymore so yeah the answer is in Swift, but it shouldn't be difficult to convert at all. The only lines you need to add are:
if NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
if NSFileManager.defaultManager().removeItemAtPath(url.path!, error: nil) {
_privatePersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if newCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) != nil {
return newCoordinator
}
}
}
It might look something like this:
if ([[NSFileManager defaultManager] fileExistsAtPath(url.path!)]) {
if ([[NSFileManager defaultManager] removeItemAtPath(url.path!, error: nil)]) {
var newCoordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
if (![_privatePersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL nil error:&error]) {
return _privatePersistentStoreCoordinator;
}
}
}