核心数据重置

时间:2013-10-14 10:20:42

标签: ios core-data nsfetchedresultscontroller

我正在使用在coreData中重置我的数据,下面是我在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]);
    }
}

此代码正常工作,表中的所有数据都将被删除。现在,当我再次尝试添加时,我收到此错误

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)

我试图调试这个问题,然后我才知道我的数据库是空的。但我不明白为什么。如果我在没有重置的情况下运行应用程序,那么evrything工作正常。

所以请帮帮我。

这是关于FRC代表的代码

- (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];
}

应用程序在[m_tableView endUpdates]行崩溃。

此致 兰吉特

1 个答案:

答案 0 :(得分:1)

我不知道这是不是原因,但是在你设定

之后
__managedObjectContext = nil;
__persistentStoreCoordinator = nil;
你打电话

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

所以如果你有getter方法(默认xcode创建它们)你再次初始化它们,如果没有,那么你在nil对象上调用方法。

编辑:尝试解释(更好)

设置

__managedObjectContext = nil;
你打电话

[self.managedObjectContext persistentStoreCoordinator]

表示

[nil persistentStoreCoordinator]

除非你有

- (NSManagedObjectContext *)managedObjectContext
课堂上的

方法。如果你有方法,它会再次为同一个(旧)数据模型创建managedObjectContext。

因此,如果你想用新的模型文件创建新的协调器,context ...,那么你需要在删除旧文件后将它们设置为nil。