内核数据复位 [英] Core Data Reset

查看:104
本文介绍了内核数据复位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用coreData重置我的数据,下面是我的代码重置我的数据在CoreData

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)

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

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.

请帮助我。

我的代码关于FRC委托

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

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

App is crashing at [m_tableView endUpdates] line.

注意
Ranjit

Regards Ranjit

推荐答案

我不知道这是不是原因,但在您设置

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

__managedObjectContext = nil;
__persistentStoreCoordinator = nil;

您呼叫

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

所以如果你有getter方法(默认xcode创建它们)你重新初始化它们,如果不是那么你调用方法对nil对象。

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

编辑:尝试解释(更好)

trying to explain (better)

设置完毕后

__managedObjectContext = nil;

您呼叫

[self.managedObjectContext persistentStoreCoordinator]

这意味着

[nil persistentStoreCoordinator]

您有

- (NSManagedObjectContext *)managedObjectContext

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

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

因此,如果你想创建新的协调器, ,那么您需要在删除旧文件后将其设置为nil。

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.

这篇关于内核数据复位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆