NSFetchedResultsController / 父子
我正在开发我的第一个核心数据支持的应用程序,并且无法弄清楚如何正确设置 NSFetchedResultsController 。我有两个实体:
/-----------\ /-----------\
| List | | ListItem |
| --------- | | --------- |
| listName | | itemName |
| --------- | | --------- |
| listItems |<---->> | list |
\ --------- / \ --------- /
我有两个视图(每个视图都继承自 UITableViewController
并且每个都由 NSFetchedResultsController
支持)。第一个视图是 UITableView
,它显示所有 List
项。当选择一行时,它会推送第二个视图。
我在视图控制器中有一个名为 @property (strong, nonatomic) List *selectedList
的属性,在推送视图之前我将其与选定的 List
一起分配。
我设置时遇到的问题是让第二个视图仅显示其父级是所选 List
的 ListItem
对象。我知道它属于 NSFetchedResultsController
中的某个位置,但我不知道在哪里。
这是我的代码:
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListItem" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Detail"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
我是否需要添加某种排序描述符,或者答案是否在于其他内容?谢谢!
I'm working on my first Core Data-backed app and am having trouble figuring out how to set up an NSFetchedResultsController properly. I have two entities:
/-----------\ /-----------\
| List | | ListItem |
| --------- | | --------- |
| listName | | itemName |
| --------- | | --------- |
| listItems |<---->> | list |
\ --------- / \ --------- /
I have two views (each inherit from UITableViewController
and each are backed by a NSFetchedResultsController
). The first view is a UITableView
that displays all of the List
items. When a row is selected, it pushes the second view.
I have a property in the view controller called @property (strong, nonatomic) List *selectedList
which I assign with the selected List
before I push the view.
What I'm having trouble setting up is having the second view only display the ListItem
objects whose parent is the selected List
. I know that this belongs somewhere in the NSFetchedResultsController
, but I don't know quite where.
Here's my code:
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListItem" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Detail"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
Do I need to add some sort of a sort descriptor, or does the answer lie with something else? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个 NSPredicate 来选择
selectedList
与其list
属性匹配的ListItem
。这里需要注意一件事,那就是谓词将根据
selectedList
发生变化,这意味着获取请求将发生变化。如果您要更改获取请求,Apple 会在NSFetchedResultsController
类参考。You need an
NSPredicate
to select theListItem
s whereselectedList
matches theirlist
property.There's one thing to be aware of here and that is that the predicate will change depending on
selectedList
meaning that the fetch request will change. If you will be changing the fetch request, Apple has this warning in theNSFetchedResultsController
Class Reference.