2013-10-14 71 views
-3

我与我重新在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]行崩溃的代码。

问候 兰吉特

+0

您仍然在'_persistentStoreCoordinator'上调用'addPersistentStore ...',您刚刚设置为nil。这条线不会做任何事情。另外,你得到的异常来自你的NSFetchedResultsController,并且你没有向我们展示任何关于它的东西。 – Abizern

+0

@Abizern,我应该使用self.persistentStoreCoordinator的权利。并检查我更新的问题。 – Ranjit

回答

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。

所以,如果你想创建新的协调器,上下文...与新的模型文件,然后你需要设置它们为零后删除旧文件。

+0

我已更新我的回答 – Mert

+0

检查我更新的问题。这是你的建议吗? – Ranjit

+0

将它们设置为nil后,只需调用self.persistentStoreCoordinator并自动添加PersistentStore,则不需要添加自己。或者你必须添加_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];在设置它们为零后,像persistentStoreCoordinator方法在appdelegate – Mert