使用 IBAction 插入数据抛出异常

Posted

技术标签:

【中文标题】使用 IBAction 插入数据抛出异常【英文标题】:Using IBAction to insert data throws exception 【发布时间】:2012-05-13 04:56:20 【问题描述】:

我想创建一个按钮,即“完成”,按下时将所有数据保存到Core Data中,我正在使用下面的代码

- (IBAction)done:(id)sender


    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.

    NSString *name = _nameField.text;
    NSString *amount = _amountField.text;

    [newManagedObject setValue:[NSDate date] forKey:@"date"];
    [newManagedObject setValue:name forKey:@"name"];
    [newManagedObject setValue:amount forKey:@"amount"];
    [newManagedObject setValue:category forKey:@"category"];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) 
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    
    [self.delegate addContentViewControllerDidSave:self];

这是获取结果控制器:

#pragma mark - fetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController 

    if (_fetchedResultsController != nil) 
        return _fetchedResultsController;
    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Item" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"date" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    //_fetchedResultsController.delegate = self;

    return _fetchedResultsController;



- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];



- (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:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray
                                               arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray
                                               arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    



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

    switch(type) 

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

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



- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];

我得到了错误:

2012-05-13 11:26:08.341 test[818:fb03] * 由于应用程序终止 未捕获的异常“NSInternalInconsistencyException”,原因: '+entityForName: 找不到实体的 NSManagedObjectModel 名称'项目''

但我检查了实体名称和 NSManagedObject 子类,它们在代码中是相同的

我尝试清理、重建和重置模拟器,但没有成功

【问题讨论】:

看看这里could-not-locate-an-nsmanagedobjectmodel-for-entity-name。希望对您有所帮助。 那么这个获取是否没有问题,只有当您尝试添加新实体对象时才会遇到问题? 您为将数据保存到实体而编写了太多代码。 【参考方案1】:

尝试从模拟器中删除应用,清理并重建它

【讨论】:

以上是关于使用 IBAction 插入数据抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

使用jsp抛出异常插入mysql数据库

当尝试使用 JPA 将数据插入 MySQL 数据库时,我被抛出异常:列 'billing_address' 不能为空

H2 db插入带引号的数据抛出异常

在java中使用CLOB抛出异常

如果 ETag 不匹配,如何使用 ETag 在插入时抛出异常(除非它是 *)

java需要h2数据库抛出jdbc异常