-3

I am working with resetting my data in coreData, below is my code to reset my data in CoreData

- (void) resetApplicationModel
{   
   __managedObjectContext = nil;
   __managedObjectModel = nil;
   __persistentStoreCoordinator = nil;
   _allPageViewController.controller = nil;


    NSError *error;
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"abc.sqlite"];


    if ([[self.managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[self.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
    {
        // remove the file containing the data
        if([[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error])
        {        

            //recreate the store like in the appDelegate method
            if([self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error] == nil)
            {
                NSLog( @"could not create new persistent store at %@ - %@", [storeURL absoluteString], [error localizedDescription]);
            }
        }
        else
        {
            NSLog( @"could not remove the store URL at %@ - %@", [storeURL absoluteString], [error localizedDescription]);
        }

    }
    else
    {
        NSLog( @"could not remove persistent store - %@", [error localizedDescription]);
    }
}

This code works properly all my data in the table is deleted. Now when I try to add again, I get this error

CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to insert row 3 into section 0, but there are only 0 rows in section 0 after the update with userInfo (null)

I tried to debug this problem, and I came to know that my database is empty. But I am not understanding why. if I run the app without reset, evrything works fine.

So please help me out.

Here is my code about FRC delegates

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{

        [m_tableView beginUpdates];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{

    if (!m_userDrivenModelChange)
    {        
        switch(type) 
        {
            case NSFetchedResultsChangeInsert:
                [m_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;

            case NSFetchedResultsChangeDelete:
                [m_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;       
        }
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{      
        switch(type) {
            case NSFetchedResultsChangeInsert:
                [m_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
                break;

            case NSFetchedResultsChangeDelete:
                [m_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;

            case NSFetchedResultsChangeMove:
                [m_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [m_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
                break;
        }    
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{

        [m_tableView endUpdates];
}

App is crashing at [m_tableView endUpdates] line.

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • You are still calling `addPersistentStore…` on `_persistentStoreCoordinator` which you have just set to nil. This line isn't going to do anything. also, the exception you are getting is from your NSFetchedResultsController, and you are not showing us anything about that. – Abizern Oct 14 '13 at 11:09
  • @Abizern, I should use self.persistentStoreCoordinator right. and check my updated question. – Ranjit Oct 14 '13 at 11:29

1 Answers1

1

I do not know if this is the reason, but after you set

__managedObjectContext = nil;
__persistentStoreCoordinator = nil;

you call

if ([[self.managedObjectContext persistentStoreCoordinator] removePersistentStore:      [[[self.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])

so if you have getter methods (as default xcode creates them) you initialize them again, if not then you call methods on nil objects.

Edit: trying to explain (better)

After you set

__managedObjectContext = nil;

you call

[self.managedObjectContext persistentStoreCoordinator]

which means

[nil persistentStoreCoordinator]

unless you have

- (NSManagedObjectContext *)managedObjectContext

method in the class. if you have the method it creates managedObjectContext for the same(old) data model again.

So if you want to create new coordinator, context... with new model file, then you need to set them nil after you remove the old file.

Mert
  • 6,025
  • 3
  • 21
  • 33
  • check my updated question. Is this what you are suggesting. – Ranjit Oct 14 '13 at 11:02
  • after you set them to nil, just call self.persistentStoreCoordinator and it will addPersistentStore automatically, you do not need to add you self. Or you have to add the line _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; after setting them to nil, like persistentStoreCoordinator method in appdelegate – Mert Oct 14 '13 at 11:17
  • I have updated my question, and I used self.persistentStoreCoordinator, but still, I get the same error – Ranjit Oct 14 '13 at 11:32
  • I tried as you said , but still the same issue. Any suggestions? – Ranjit Oct 14 '13 at 12:42