HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios NSFetchRequest,命令子对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您想知道如何指定Fetchrequest,我可以在关系中订购对象.

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

例如,如果我有一个包含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];

干杯

解决方法

这有两种策略:

>使用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,命令子对象所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。