Jordan Smith
Jordan Smith

Reputation: 10378

Deleted NSData reappears after app is restarted

In my app, I have a 'delete all' button in the settings menu, which deletes all stored user data. The following code is used:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

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

for (NSManagedObject *managedObject in items) {
    [managedObjectContext deleteObject:managedObject];
}

This seems to work well, and all data seems to vanish from the app. That is, until the app is closed and restarted...

Opening the app from a backgrounded state does not do this, but when the app is force quit via the multitasking switcher, all the 'deleted' data reappears when the app is restarted.

-

All data actually seems to disappear when deleted, searching for any data etc returns no results. What am I doing wrong in the way I am deleting the data? Any help is much appreciated!

Upvotes: 2

Views: 352

Answers (1)

JustSid
JustSid

Reputation: 25318

Don't forget to save the context before opening it again, otherwise all changes will remain unsaved.

Upvotes: 8

Related Questions