ios - Core Data: 一个多对多关联的查询问题
黄舟
黄舟 2017-04-17 11:13:31
[iOS讨论组]

我有两个类,ClassA和ClassB,关系如下:

我需要查询所有count>10的ClassA的bObjects中name为hello的ClassB的对象。

请问能不能用一个NSPredicate来表示这个查询?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(2)
PHPz

看了苹果的文档,里面写的例子:

If you use a to-many relationship, the construction of a predicate is slightly different. If you want to fetch Departments in which at least one of the employees has the first name "Matthew," for instance, you use an ANY operator as shown in the following example:
 NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"ANY employees.firstName like 'Matthew'"];

所以,我觉得查询应该是:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (ANY aObjects.count > 10)", @"hello"];
怪我咯

脑补的,没测试。。。不好用请更正我

NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"ClassB"
                                   inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (aObjects.count > 10)", @"hello"];
[fetchRequest setPredicate:predicate];
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号