ios NSFetchRequest,排序子对象

发布于 2024-11-01 13:41:50 字数 1136 浏览 0 评论 0原文

您好,我想知道如何指定 FetchRequest,我可以在其中对关系中的对象进行排序。

   |  Parent         |        | Child        |
   |     - name      |------->|   - name     |
   |     - position  |        |   - position |

例如,如果我有一个包含位置属性的父表,并且与也具有位置属性的子表具有一对多关系。如何返回包含按位置排序的子对象的父对象(按位置排序)。

例如

parent 1
   child 1
   child 2
   child 3

parent 2
   child 15
   child 16

parent 3
   child 22
   child 23
   child 24

,显然下面的代码将正确排序父对象,但是我如何使每个父对象返回的子对象按正确的顺序

    NSFetchRequest* fetchReqest = [[NSFetchRequest alloc] init];

    NSEntityDescription* entity = [NSEntityDescription entityForName:@"parent"  inManagedObjectContext:managedObjectContext];
    [fetchReqest setEntity:entity];  

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"position" ascending:YES];
    [fetchReqest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];

    NSArray* parentsThatContainChildren =  [managedObjectContext executeFetchRequest:fetchReqest error:nil];

干杯

Hi would like to know how to specify a FetchRequest where i can order the objects in a relationship.

   |  Parent         |        | Child        |
   |     - name      |------->|   - name     |
   |     - position  |        |   - position |

For example if i have a parent table that contains a position attribute and has a one to many relationship with a child table that has a also has a position attribute. How do i return the parent objects (ordered by position) that contain the child objects ordered by position.

e.g

parent 1
   child 1
   child 2
   child 3

parent 2
   child 15
   child 16

parent 3
   child 22
   child 23
   child 24

Obviously the below code will order the parent objects correctly, but how do i make the child objects that are returned with the each parent to be in the correct order

    NSFetchRequest* fetchReqest = [[NSFetchRequest alloc] init];

    NSEntityDescription* entity = [NSEntityDescription entityForName:@"parent"  inManagedObjectContext:managedObjectContext];
    [fetchReqest setEntity:entity];  

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:@"position" ascending:YES];
    [fetchReqest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];

    NSArray* parentsThatContainChildren =  [managedObjectContext executeFetchRequest:fetchReqest error:nil];

Cheers

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

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

发布评论

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

评论(1

染墨丶若流云 2024-11-08 13:41:50

有两种策略可以实现这一点:

  1. 使用 NSSortDescriptor 为子级执行单独的获取请求。优点:您将其保存在 FetchController 中。缺点:多个获取请求会减慢您的速度
  2. 对 NSArray*parentsThatContainChildren 中返回的子项进行排序。

对于#2,您可以查看一下

NSSortDescriptor *positionSort = [NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES];

NSArray *children = [[parent.children allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:positionSort]];

There's 2 strategies to this:

  1. Perform a separate fetch request for the children with the NSSortDescriptor. Pro: you keep this in the FetchController. Con: multiple fetch requests can slow you down
  2. Sort the returned children in the NSArray* parentsThatContainChildren.

For #2, you can check this out :

NSSortDescriptor *positionSort = [NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES];

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