Dipti Y W
Dipti Y W

Reputation: 474

Deleting all table records from core data database using relationship

I'm using core data for my application i have 4-5 tables one of which is userProfile table. i have implemented logout in the app. if user logs out of the app im deleting user profile and will be inserting new if logged in with other user account. i want to delete all the records from database on userprofile delete. im using relationship for this but it is not deleting other records from the db even if user profile record has been deleted. one thing i would like to mention is all the data is coming from service. and i am using cascade delete rule for relationship created between userprofile table and other tables.

Upvotes: 3

Views: 3444

Answers (2)

Dipti Y W
Dipti Y W

Reputation: 474

I tried using relationship using cascade rule but didn't worked for me so I used Delete/Reset all entries in Core Data method. Used following code for this.

NSError * error; 
NSURL * storeURL = [[AppDelegate.managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[AppDelegate.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
[AppDelegate.managedObjectContext lock];
[AppDelegate.managedObjectContext reset];//to drop pending changes 
//delete the store from the current managedObjectContext    
if ([[AppDelegate.managedObjectContext persistentStoreCoordinator] removePersistentStore: [[[AppDelegate.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])    
{        
// remove the file containing the data   
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];    
//recreate the store like in the  appDelegate method   
[[[AppDelegate managedObjectContext] persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options: @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];  
}    
[AppDelegate.managedObjectContext unlock];   
//that's it !  

This worked for me.

Upvotes: 4

Lorenzo B
Lorenzo B

Reputation: 33428

You have two different ways to achieve this.

The first is to delete the store and create it again. This means access the store in the file system and delete the sql file, for example. For example, you can find how to achieve it in the following discussion: Delete/Reset all entries in Core Data?.

The second is solution is to create a cascade relationship in UserProfile entity that will link the other ones. In the latter you must set up an inverse relationship (nullify would be the correct approach). For further info see my answer at Setting up a parent-child relationship in Core Data.

Said this, and based on my experience, I would discourage to save user info (e.g. passwords) in Core Data. Instead, adopt the Keychain for this. There are libraries that wrap the Keychain access in an easy manner (e.g. SSKeychain).

Upvotes: 4

Related Questions