RRK

Erase CoreData

RRK
Sep 2nd, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //appdelegate.m
  2. - (void) resetApplicationModel
  3. {
  4.     __managedObjectContext = nil;
  5.     __managedObjectModel = nil;
  6.     __persistentStoreCoordinator = nil;
  7.    // _allPageViewController.controller = nil;
  8.    
  9.     NSError *error;
  10.     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"abc.sqlite"];
  11.    
  12.    
  13.     if ([[self.managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[self.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
  14.     {
  15.         // remove the file containing the data
  16.        
  17.         if([[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error])
  18.         {
  19.             __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  20.             if([__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error] == nil)
  21.             {
  22.                 NSLog( @"could not create new persistent store at %@ - %@", [storeURL absoluteString], [error localizedDescription]);
  23.             }
  24.         }
  25.         else
  26.         {
  27.             NSLog( @"could not remove the store URL at %@ - %@", [storeURL absoluteString], [error localizedDescription]);
  28.         }
  29.        
  30.     }
  31.     else
  32.     {
  33.         NSLog( @"could not remove persistent store - %@", [error localizedDescription]);
  34.     }
  35.    
  36.      [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotificationDestroyAllNSFetchedResultsControllers" object:self];
  37. }
  38.  
  39. //RootViewController
  40. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  41. {
  42.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  43.     if (self) {
  44.  
  45.      
  46.         [[NSNotificationCenter defaultCenter] addObserverForName:@"kNotificationDestroyAllNSFetchedResultsControllers" object:nil queue:nil usingBlock:^(NSNotification *note) {
  47.             NSLog(@"[%@] must destroy my nsfetchedresultscontroller", [self class]);
  48.             //m_controller = nil;
  49.        
  50.        
  51.        
  52.             NSError *error;
  53.             if (![m_controller performFetch:&error]) {
  54.                 /*
  55.                  Replace this implementation with code to handle the error appropriately.
  56.        
  57.                  abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  58.                  */
  59.                 NSLog(@"Unresolved error %@, %@", error, [error localizedDescription]);
  60.                 abort();
  61.             }
  62.                        
  63.             }];
  64.     }
  65.     return self;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment