1

I have followed a variety of posts here in SO to delete all the data from an app so I can start over. I have tried:

A) Deleting all the data:

NSArray *entities = model.entities;
for (NSEntityDescription *entityDescription in entities) {
    [self deleteAllObjectsWithEntityName:entityDescription.name
                               inContext:context];
}
if ([context save:&error]) {
   ...

- (void)deleteAllObjectsWithEntityName:(NSString *)entityName
                         inContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *fetchRequest =
    [NSFetchRequest fetchRequestWithEntityName:entityName];
    fetchRequest.includesPropertyValues = NO;
    fetchRequest.includesSubentities = NO;

    NSError *error;
    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *managedObject in items) {
        [context deleteObject:managedObject];
        NSLog(@"Deleted %@", entityName);
    }
}

B) Delete the physical data store:

NSError *error;
NSPersistentStore *store = [[self persistentStoreCoordinator].persistentStores lastObject];
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = store.persistentStoreCoordinator;
[self.diskManagedObjectContext reset]; // there is a local instance variable for the disk managed context
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
_diskManagedObjectContext = nil;

C) Perform step A and then step B

In all combinations it appears to run with no errors, but whenever I receive new data (via my HTTP service) and start adding it to the re-initialized data store I get all kinds of duplicate data and various data issues. I usually have to delete and reinstall the app to get the data clean enough to re-initialize.

It should be fairly straightforward. The user logs in. App data is downloaded and saved in the store. User logs out and logs in again or as different ID and new data is brought down.

Any ideas why the above methods are not working?

UPDATE:

I edited my code above to show that I am saving the context and removing the data store file. I still end up with bad leftover data. Could the problem be the multiple contexts we use? We have three contexts we use in the app: a UI-managed context, a background context and a disk-managed context. A notification listener takes care of merging changes in the background context with the disk managed context.

I have tried altering the above code to loop through the objects in all three contexts and we set them all to nil. The authentication code takes care of reinitializing the contexts. Still banging my head on what seems like a simple issue.

davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Are you saving context? – Zia Apr 22 '14 at 19:48
  • In A) after deleting all the objects, you don't appear to be saving the managed object context. A better approach overall might be to just make sure you don't have any objects loaded, delete the context and persistent store, delete the file(s) and start over, creating new context and persistent stores. – David Berry Apr 22 '14 at 19:49
  • possible duplicate of [Core Data: Quickest way to delete all instances of an entity](http://stackoverflow.com/questions/1383598/core-data-quickest-way-to-delete-all-instances-of-an-entity) – davidethell Apr 24 '14 at 21:07
  • We should close this question. I found my answer and it ended up not being a data deletion problem. So let's close this as a dupe of other delete all objects questions. – davidethell Apr 24 '14 at 21:07

2 Answers2

0

After

for (NSEntityDescription *entityDescription in entities) {
[self deleteAllObjectsWithEntityName:entityDescription.name
                           inContext:context];
}

Save your context

[context save:&error];
Zia
  • 14,622
  • 7
  • 40
  • 59
  • Sorry about that miscommunication in my question. I am saving the context, but forgot to include that in the code. – davidethell Apr 23 '14 at 09:14
0

(B) doesn't delete the physical store, it just dissociates it from your app for the time being. No doubt you just attach it again shortly thereafter or upon next launch.

Use [[NSFileManager defaultManager] removeItemAtURL:... error:...] actually to delete the file from your disk.

As the other posters have said, you fail to NSManagedObjectContext -save: in (A) so you affect what's in that one context but not in the persistent store. Contexts are just in-memory scratch pads so as soon as you create a new context it'll be able to find everything in the persistent store again unless or until you save the one with the modifications.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • I'll check this out. I did have removeItemAtURL in the code at one point, but it is commented out at the current time. I'll uncomment, test and get back. – davidethell Apr 23 '14 at 09:15
  • I added back in the removal of the file and I still have no better results. Post edited above. – davidethell Apr 23 '14 at 09:56
  • So you're saying that there's no persistent store on disk at all, you've emptied out your context, but the data is magically reappearing from nowhere? – Tommy Apr 23 '14 at 13:36
  • No, the data seems emptied, but when I fetch the new data from the server I get duplicated records and a mix of old and new data. Sometimes I get just a few dupes, sometimes a dozen. – davidethell Apr 23 '14 at 14:26
  • I should clarify that the data coming from our http service is well-formed, but after importing we see all kinds of data issues that aren't present on a fresh install and import. – davidethell Apr 23 '14 at 14:31