NSFetchedResultsController需要非零提取请求和managedObjectContext错误

时间:2012-05-06 13:24:34

标签: ios core-data nsmanagedobjectcontext nsfetchedresultscontroller

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

我是核心数据的新手并且真的在努力应对这个错误,所以我将不胜感激。实现文件具有以下代码

- (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];
    }
}

1 个答案:

答案 0 :(得分: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的课程参考。

希望它有所帮助。