gpt4 book ai didi

ios - NSFetchedResultsController 需要非零获取请求和 ManagedObjectContext 错误

转载 作者:行者123 更新时间:2023-11-29 04:40:28 24 4
gpt4 key购买 nike

在具有选项卡式 View 的应用程序上使用核心数据。第二个选项卡加载核心数据,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];
}
}

最佳答案

您确定正确设置了托管对象上下文吗?看起来 fetch 请求没问题,但是上下文呢?如果上下文设置不正确,可能会导致该错误。

您是否从外部对象注入(inject)上下文?如果是,如何声明 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;

您还可以从应用程序委托(delegate)中获取 Controller 内的主上下文(如果您已在那里声明),如下所示:

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

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

一些注释

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

这里是 NSFetchedResultsControllerDelegate 的类引用.

希望对您有所帮助。

关于ios - NSFetchedResultsController 需要非零获取请求和 ManagedObjectContext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10470857/

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