Reputation: 515
I am fighting with CoreData again and i need your help :P
I am writing a RSS reader. Design is based on Apple TopSongs example using libxml2 library.
When my application is up I am checking to see when was the last update. If it was more than 1 hour ago i remove the current Persistent store and download everything again.
if ([[NSFileManager defaultManager] fileExistsAtPath:self.persistentStorePath]) {
NSError *error = nil;
BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:self.persistentStorePath error:&error];
NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
}
I can see that the local sql file was deleted.
Now i am creating some NSOperations to download information from the feeds in the background.
So far everything works great.
Like every RSS feed, there is an update button which suppose to check for changes and update the coreData. For starter i decided i will delete everything and re-download everything (not efficent i know, but the RSS feed does not have guid and i need to implement a hash function to figure if there were any changes).
So what i'm doing is basically calling the same code as above to delete the store.. re-downloading all RSS feeds again (same logic when the application was first loaded)
I am reloading the tables but i can still see the same data on the tableView, everything remained untouched.
I decided to go even further, and to delete the store without downloading content.
if ([[NSFileManager defaultManager] fileExistsAtPath:app.persistentStorePath]) {
NSError *error = nil;
BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:app.persistentStorePath error:&error];
NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
}
[self.managedObjectContext save:&err];
[self.fetchedResultsController performFetch:&error];
[self.tableView reloadData]
I expected to see empty tables but the data was still there.
How is this possible ? i deleted the store, re-fetched and reloaded the data.
What am i doing wrong?
Upvotes: 0
Views: 633
Reputation: 1718
I think your problem might be with the line
[self.managedObjectContext save:&err];
Rather follow @Ladislav's suggestion:
NSFetchRequest * allObjectsRequest = [[NSFetchRequest alloc] init];
[allObjectsRequest setEntity:[NSEntityDescription entityForName:@"Object" inManagedObjectContext:context]];
[allObjectsRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:allObjectsRequest error:&error];
[allObjectsRequest release];
//error handling goes here
for (NSManagedObject * object in objects) {
[context deleteObject:object];
}
[context save:&error];
Upvotes: 1
Reputation: 7283
Hey, don't delete the whole Core Data database...delete every entry in the Core Data database and not the whole base itself. You can delete an object from Core Data database by:
[context deleteObject:aCoreDataObject];
Oh one more thing...if you change your Core Data database, that won't be reflected in the tableview. If you want to see changes you need to call for example [tableView reloadData];
Upvotes: 1