NSFetchedResultsController / 父子

发布于 2024-12-22 03:33:37 字数 2487 浏览 2 评论 0原文

我正在开发我的第一个核心数据支持的应用程序,并且无法弄清楚如何正确设置 NSFetchedResultsController 。我有两个实体:

/-----------\        /-----------\
| List      |        | ListItem  |
| --------- |        | --------- |
| listName  |        | itemName  |
| --------- |        | --------- |
| listItems |<---->> | list      |
\ --------- /        \ --------- /

我有两个视图(每个视图都继承自 UITableViewController 并且每个都由 NSFetchedResultsController 支持)。第一个视图是 UITableView,它显示所有 List 项。当选择一行时,它会推送第二个视图。

我在视图控制器中有一个名为 @property (strong, nonatomic) List *selectedList 的属性,在推送视图之前我将其与选定的 List 一起分配。

我设置时遇到的问题是让第二个视图仅显示其父级是所选 ListListItem 对象。我知道它属于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-12-29 03:33:37

您需要一个 NSPredicate 来选择 selectedList 与其 list 属性匹配的 ListItem

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list == %@", self.selectedList];
[fetchRequest setPredicate:predicate];

这里需要注意一件事,那就是谓词将根据 selectedList 发生变化,这意味着获取请求将发生变化。如果您要更改获取请求,Apple 会在 NSFetchedResultsController 类参考

您不能简单地更改获取请求来修改结果。如果
您想要更改获取请求,您必须:

  1. 如果您正在使用缓存,请将其删除(使用 +deleteCacheWithName:)。

  2. 通常,如果您要更改提取请求,则不应使用缓存。

  3. 更改获取请求。调用-performFetch:

You need an NSPredicate to select the ListItems where selectedList matches their list property.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list == %@", self.selectedList];
[fetchRequest setPredicate:predicate];

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 the NSFetchedResultsController Class Reference.

You cannot simply change the fetch request to modify the results. If
you want to change the fetch request, you must:

  1. If you are using a cache, delete it (using +deleteCacheWithName:).

  2. Typically you should not use a cache if you are changing the fetch request.

  3. Change the fetch request. Invoke -performFetch:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文