0

在具有选项卡式视图的应用程序上使用核心数据。第二个选项卡加载核心数据,Xcode 中或运行应用程序时没有显示错误,但是当我单击第二个选项卡时,应用程序崩溃并出现错误“NSFetchedResultsController 的实例需要非零获取请求和 managedObjectContext。

我是核心数据的新手,并且真的在为这个错误而苦苦挣扎,所以我会很感激我能得到的任何帮助。实现文件有以下代码

- (void)setupFetchedResultsController
{
    // 1 - Decide what Entity you want
    NSString *entityName = @"EatCategory"; // Put your entity name here
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    // 2 - Request that Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

    // 3 - Filter it if you want
    //request.predicate = [NSPredicate predicateWithFormat:@"EatCategory.name = Blah"];

    // 4 - Sort it if you want
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                                     ascending:YES
                                                                                      selector:@selector(localizedCaseInsensitiveCompare:)]];
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    [self performFetch];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupFetchedResultsController];

    if ([[self.fetchedResultsController fetchedObjects] count] == 0) {
        NSLog(@"No Results were fetched so nothing will be given to the table view");
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Eat Category Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    // configure the cell...
    EatCategory *eatcategory = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = eatcategory.name;

    return cell;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView beginUpdates]; // Avoid  NSInternalInconsistencyException

        // Delete the role object that was swiped
        EatCategory *eatCategoryToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSLog(@"Deleting (%@)", eatCategoryToDelete.name);
        [self.managedObjectContext deleteObject:eatCategoryToDelete];
        [self.managedObjectContext save:nil];

        // Delete the (now empty) row on the table
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self performFetch];

        [self.tableView endUpdates];
    }
}
4

1 回答 1

1

您确定正确设置托管对象上下文吗?似乎获取请求没问题,但是上下文呢?如果上下文设置不正确,则可能导致该错误。

您是否从外部对象注入上下文?如果是,如何声明managedObjectContext财产?

例如:

// from an external object
YourController *controller = ... // alloc-init the controller
controller.managedObjectContext = self.managedObjectContext;

// within your controller .h
@property (strong, nonatomic) NSManagedObject* managedObjectContext; // or retain if you don't use ARC

// within your controller .m
@synthesize managedObjectContext;

您还可以从应用程序委托(如果您已在其中声明)获取控制器内部的主要上下文,如下所示:

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* managedObjectContext = delegate.managedObjectContext;

但这可能导致更严格的应用程序设计。

一些笔记

而不是使用commitEditingStyle你可以“注册”NSFetchedResultsControllerDelegate回调。创建此类以处理表视图中的更改。您可以使用它并以不同的方式响应表格更改。

这里是NSFetchedResultsControllerDelegate的类参考。

希望能帮助到你。

于 2012-05-06T14:45:29.267 回答