gpt4 book ai didi

ios - 尝试保存 NSManagedObject 时崩溃

转载 作者:行者123 更新时间:2023-11-29 12:33:01 26 4
gpt4 key购买 nike

大家好!

我有一个 UITableView。我需要向它添加一些对象(具有一些属性,在这种情况下我至少需要名称),当我在导航栏上点击“+”时,新的 UIViewController 以模态方式呈现,在 viewDidLoad 中我调用此方法,设置默认名称对它:

- (void)createManagedObject {

NSManagedObjectContext *context = [[DVCoreDataManager sharedManager] managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"DVObject" inManagedObjectContext:context];

[newManagedObject setValue:@"new object" forKey:@"name"];

NSError *error = nil;
if (![context save:&error]) {
NSLog(@"%@", [error localizedDescription]);
}

self.currentObjectID = newManagedObject.objectID;
}

在 UITextField 中输入名称后,我点击“保存”按钮并调用该方法,我需要更新对象的名称:

- (IBAction)actionSaveTraining:(UIBarButtonItem *)sender {

NSManagedObjectContext *context = [[DVCoreDataManager sharedManager] managedObjectContext];
NSManagedObject *newManagedObject = [context existingObjectWithID:self.currentObjectID error:nil];

[newManagedObject setValue:self.nameTextField.text forKey:@"name"];

NSError *error = nil;
if (![context save:&error]) {
NSLog(@"%@", [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
}

此时应用程序因错误而崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我实现了 NSFetchedResultsControllerDelegate 方法。controllerDidChangeContent: 在 createManagedObject 方法之后被调用,但 controllerWillChangeContext: 从未被调用,我不明白为什么。

- (void)controllerWillChangeContext:(NSFetchedResultsController *)controller {

[self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeMove:
break;

case NSFetchedResultsChangeUpdate:
break;
}
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

default:
break;
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

[self.tableView endUpdates];
}

我该如何解决这个错误?也许有更好的方法来做我需要的? (在创建和保存时设置默认名称,从 uitextfield 设置名称)

最佳答案

您错误地输入了controllerWillChangeContent 方法的名称:最后一个字应该是content 而不是context:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

[self.tableView beginUpdates];
}

关于ios - 尝试保存 NSManagedObject 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com