ioscore-datanspersistentstore

How to delete selected tables using persistentStoreCoordinator


I'm currently working on an IOS app which is already developed using objective-C. I have added a module where users when login store details about the user. But as the app is already having some code, when I press the logout button it deletes all the entities from the database. For this they are using something like the code below.

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

NSError *error = nil;

// retrieve the store URL

NSURL *storeURL = [[managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];

// lock the current context

[managedObjectContext lock];

[managedObjectContext reset];//to drop pending changes

//delete the store from the current managedObjectContext

if ([[managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[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

    [[managedObjectContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];//recreates the persistent store

}

[managedObjectContext unlock];

By keeping the break points, I understood that they are retrieving the url of the database and deleting it and re-creating it. Lets say they have 3 tables A,B and C, I want to delete A & B but not C. Reference- Persistent Store Coordinator

Is my understanding correct? How can I achieve this?

TIA


Solution

  • I did something like below. Correct me If I'm wrong with my approach.

        - (void) deleteData {
    
            NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
            NSError *error = nil;
    
            [managedObjectContext lock];
            [managedObjectContext reset];
            NSPersistentStoreCoordinator *psc = [managedObjectContext persistentStoreCoordinator];
            NSManagedObjectModel *managedModel = [psc managedObjectModel];
            NSArray *allEntityNames = [managedModel.entitiesByName allKeys];
    
            for(NSString *entityName in allEntityNames)
            {
    //I wanted to delete all objects except for one table
                if(![entityName isEqual:switchAccountsTableName])
                {
                    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
                    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
                    [fetchRequest setEntity:entity];
                    NSError *objError = nil;
                    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&objError];
                    if(fetchedObjects == nil)
                    {
                        NSLog(@"Logout- Couldnt delete entity objects");
                    }
                    for(NSManagedObject *entityObj in fetchedObjects)
                    {
                        [managedObjectContext deleteObject:entityObj];
                    }
                }
            }
            [managedObjectContext save:&error];
            [managedObjectContext unlock];
        }