2

I am trying to reset my Core data database when the user sets up there current model.

I have tried various options but none have correctly worked.

I want the Core data and all it's data (Including persistent data) to be deleted before any new data is added.

I have been using SQLite database browser Version 2.0b1 to check that the data has correctly been deleted. Also I have reset the simulator before running each test.

I am looking for a concise solution, I have searched over stackoverflow to find a correct method of doing it.

This solution resulted in the database being completely removed but no new one created. https://stackoverflow.com/a/11812997/1565615

This solution didn't delete all of the data as previous sessions still existed and data that had just been created. https://stackoverflow.com/a/1078306/1565615

Community
  • 1
  • 1
StackRunner
  • 1,463
  • 2
  • 16
  • 22

1 Answers1

2

i am using it like this:

  1. delete all values in coreData
  2. save the context, so the values are really deleted.
  3. save your new values and save the context again.

  1. i am using this code to delete the values from each entity:

NSError *error = nil;  

NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchREntityUser            = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entityUser         = [NSEntityDescription entityForName:@"EntityUser" inManagedObjectContext:context];

[fetchREntityUser           setEntity:entityUser];


NSArray *fetchedObjUser         = [context executeFetchRequest:fetchREntityUser error:&error];

if (error) {
    XLog(@"deleteCoreDataEntitys Error: %@", [error localizedDescription]);
}


for (NSManagedObject *CDentityUser in fetchedObjUser) {
    [context deleteObject:CDentityUser];
}


[context save:&error];
brush51
  • 5,691
  • 6
  • 39
  • 73
  • 2012-08-30 11:49:07.953 NameOfApp SSI[2019:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'CurrentManagement'' *** First throw call stack: (0x165b022 0x1350cd6 0x1e6d47 0x6aff 0x6e68 0x6ea5 0x165ce99 0x4b614e 0x4b60e6 0x55cade 0x55cfa7 0x55c266 0x4db3c0 0x4db5e6 0x4c1dc4 0x4b5634 0x2467ef5 0x162f195 0x1593ff2 0x15928da 0x1591d84 0x1591c9b 0x24667d8 0x246688a 0x4b3626 0x1ced 0x1c55) terminate called throwing an exception(lldb) – StackRunner Aug 30 '12 at 10:50
  • Thats what happens when I use that code also Autorelease not part of ARC so I remove it , and what is XLog ? Thanks again. – StackRunner Aug 30 '12 at 10:51
  • 1
    change XLog to NSLog. or delete the line completely, you dont need it. XLog is a better way of NSLog. see here: http://stackoverflow.com/a/12194649/558150 . have you deleted the sqlite file? your model couldnt locate. Check the correct name of your entity. is CurrentManagement the name of your entity or of your sqlite file? **it should be the name of your Entity**. – brush51 Aug 30 '12 at 10:55
  • It was an issue with where I was getting my context from changed it to my AppDelegate and now it works. I have accepted your answer. – StackRunner Aug 30 '12 at 10:55
  • 1
    and please also see this link, maybe you have put that code in your viewDidLoad: http://stackoverflow.com/a/1640224/558150 – brush51 Aug 30 '12 at 10:56