ios NSFetchRequest,命令子对象

ios NSFetchRequest,命令子对象,第1张

概述您想知道如何指定FetchRequest,我可以在关系中订购对象. | Parent | | Child | | - name |------->| - name | | - position | | - position | 例如,如果我有一个包含position属性的父表,并且与 您想知道如何指定FetchRequest,我可以在关系中订购对象.

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

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

例如

parent 1   child 1   child 2   child 3parent 2   child 15   child 16parent 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];

干杯

解决方法 这有两种策略:

>使用NSSortDescriptor为子项执行单独的获取请求.亲:你把它保存在FetchController中. Con:多次获取请求可能会降低您的速度
>在NSArray * parentsThatContainChildren中对返回的子项进行排序.

对于#2,你可以check this out:

NSSortDescriptor *positionSort = [NSSortDescriptor sortDescriptorWithKey:@"position" ascending:YES];NSArray *children = [[parent.children allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:positionSort]];
总结

以上是内存溢出为你收集整理的ios NSFetchRequest,命令子对象全部内容,希望文章能够帮你解决ios NSFetchRequest,命令子对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1051238.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存