【问题标题】:Core Data Reset [duplicate]核心数据重置 [重复]
【发布时间】:2013-10-21 21:24:52
【问题描述】:

我正在重置我在 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)

我尝试调试这个问题,发现我的数据库是空的。但我不明白为什么。如果我在不重置的情况下运行应用程序,一切正常。

所以请帮帮我。

这是我关于 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] 行崩溃。

问候 兰吉特

【问题讨论】:

  • 您仍在调用您刚刚设置为零的_persistentStoreCoordinator 上的addPersistentStore…。这条线不会做任何事情。此外,您得到的异常来自您的 NSFetchedResultsController,而您没有向我们展示任何相关信息。
  • @Abizern,我应该正确使用 self.persistentStoreCoordinator。并检查我更新的问题。

标签: ios core-data nsfetchedresultscontroller


【解决方案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。

因此,如果您想使用新模型文件创建新的协调器、上下文...,那么您需要在删除旧文件后将它们设置为零。

【讨论】:

  • 我已经更新了答案
  • 检查我更新的问题。这是您的建议吗?
  • 设置为nil后,调用self.persistentStoreCoordinator,它会自动添加PersistentStore,不需要添加self。或者您必须添加行 _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];将它们设置为 nil 后,如 appdelegate 中的 persistentStoreCoordinator 方法
  • 我已经更新了我的问题,并且我使用了 self.persistentStoreCoordinator,但我仍然得到同样的错误
  • 我按照你说的试过了,但还是同样的问题。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多