我與我重新在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]行崩潰的代碼。
問候 蘭吉特
您仍然在'_persistentStoreCoordinator'上調用'addPersistentStore ...',您剛剛設置爲nil。這條線不會做任何事情。另外,你得到的異常來自你的NSFetchedResultsController,並且你沒有向我們展示任何關於它的東西。 – Abizern
@Abizern,我應該使用self.persistentStoreCoordinator的權利。並檢查我更新的問題。 – Ranjit