In my ios app, I am using Magical Record and NSFetchedResultsController. I am trying to implement below functionality:
- User navigates to settings screen
- He selects - 'Delete Account'
- All his data is deleted
- He is navigated to re-registration screen
To delete all his data I wrote below code:
- (void)cleanAndResetupDB
{
[MagicalRecord cleanUp];
BOOL isSuccess = YES;
for (NSString *dbStore in [self dbBackups]) {
NSError *error = nil;
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:dbStore];
if(![[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error]){
NSLog(@"An error has occurred while deleting %@", dbStore);
NSLog(@"Error description: %@", error.description);
isSuccess = NO;
}
}
if (isSuccess) {
[MagicalRecord setupCoreDataStackWithStoreNamed:CRP_DB];
}
}
- (NSArray *)dbBackups
{
NSString *shmFileName = [NSString stringWithFormat:@"%@-shm",CRP_DB];
NSString *walFileName = [NSString stringWithFormat:@"%@-wal",CRP_DB];
return @[CRP_DB,shmFileName,walFileName];
}
When registration is complete user is navigated to contacts screen, where we retrieve related contacts from server and store it in local DB. Since FRC is used to retrieve data from local DB and show it in table view, as soon as data is saved in db it automatically appears in table view.
Problem is- If I quit the app after removing local db, on relaunch it shows proper records, but if I don't quit the app after removing local db, then it shows duplicate records.
Any clues?