iPhone coredata 获取请求,关系和部分的基础知识

发布于 2024-08-11 16:29:12 字数 1462 浏览 7 评论 0原文

我有一个希望是一个简单的问题 - 我有两个实体 - List 和 ListItem - 并且它们之间建立了一对多关系,一切都很好。

当我尝试执行 fetchrequest 时,我的问题出现了,该请求将返回按列表项的属性划分的列表项。我无法直接在 listItem 对象上执行 fetchrequest,因为没有办法说“Where List = 'xxxxx'”,所以我有类似以下内容:


- (NSFetchedResultsController *)getListItems {
    // Init a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"List" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listItem.productName" ascending:YES selector:nil];
    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
    [fetchRequest setSortDescriptors:descriptors];


    // Init the fetched results controller
    NSError *error;
    self.globalFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"listItem.productName" cacheName:@"listItems"];
    self.globalFetchedResultsController.delegate = self;
    if (![[self globalFetchedResultsController] performFetch:&error])   NSLog(@"Error: %@", [error localizedDescription]);

    [fetchRequest release];
    [sortDescriptor release];

    return self.globalFetchedResultsController;
}

现在,这个错误是因为它说我不能使用一个 -排序描述符中的对多关系 - 但它也要求我有一个排序描述符,所以我不确定执行此操作的正确方法。任何帮助都会很棒。

I've got what is hopefully a simple question - I've got two entities - List and ListItem - and there's a one-to-many relationship set up between them, all good.

My problem comes when I'm trying to perform a fetchrequest which will return the listitems sectioned up by an attribute of the listitem. I can't perform a fetchrequest directly on the listItem objects because there's no way to say 'Where List = 'xxxxx'', so I had something like the following:


- (NSFetchedResultsController *)getListItems {
    // Init a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"List" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listItem.productName" ascending:YES selector:nil];
    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
    [fetchRequest setSortDescriptors:descriptors];


    // Init the fetched results controller
    NSError *error;
    self.globalFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"listItem.productName" cacheName:@"listItems"];
    self.globalFetchedResultsController.delegate = self;
    if (![[self globalFetchedResultsController] performFetch:&error])   NSLog(@"Error: %@", [error localizedDescription]);

    [fetchRequest release];
    [sortDescriptor release];

    return self.globalFetchedResultsController;
}

Now, this errors because it's saying I can't use a one-to-many relationship in the sort descriptor - but it also requires that I have a sort descriptor, so I'm not sure the correct way to be doing this. Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜`诱少女 2024-08-18 16:29:12

如果您想获取并分段 ListItems,那么您应该使用“ListItem”作为获取的实体。

在这种情况下,有一种说法是“获取所有 List = 'xxxxx' 的 ListItem 对象”,它称为谓词(NSPredicate)。

尝试这样的操作(其中“list”是 ListItem 与 List 的关系的名称):

NSPredicate *pred = [NSPredicate predicateWithFormat:@"list == %@", listObject];
[fetchRequest setPredicate:pred];

另外,请参阅 谓词编程指南

If you want to fetch and section ListItems, then you should be using "ListItem" as your entity for the fetch.

In this case, there is a way to say "fetch all ListItem objects Where List = 'xxxxx'", it's called a predicate (NSPredicate).

Try something like this (where "list" is the name of the ListItem relationship with List):

NSPredicate *pred = [NSPredicate predicateWithFormat:@"list == %@", listObject];
[fetchRequest setPredicate:pred];

Also, see the Predicate Programming Guide.

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