Skip to content

Instantly share code, notes, and snippets.

@binho
Created April 2, 2015 19:48
Show Gist options
  • Save binho/e8d0109dc48741940ef4 to your computer and use it in GitHub Desktop.
Save binho/e8d0109dc48741940ef4 to your computer and use it in GitHub Desktop.
MagicalRecord remove and reset CoreData
- (void)resetCoreData {
DDLogInfo(@"[RESET-COREDATA] Started.");
[MagicalRecord cleanUp];
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:kMobilStoreName];
NSURL *walURL = [[storeURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"sqlite-wal"];
NSURL *shmURL = [[storeURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"sqlite-shm"];
NSError *error = nil;
BOOL hadSuccess = YES;
for (NSURL *url in @[storeURL, walURL, shmURL]) {
if ([[NSFileManager defaultManager] fileExistsAtPath:url.path]) {
hadSuccess = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
}
}
if (hadSuccess) {
DDLogInfo(@"[RESET-COREDATA] Reset OK, recreating...");
[MagicalRecord setupCoreDataStackWithStoreNamed:kMobilStoreName];
}
else {
DDLogError(@"[RESET-COREDATA] An error has occurred while deleting %@ error: %@", kMobilStoreName, error);
}
DDLogInfo(@"[RESET-COREDATA] Finished");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment