如何计算 NSFetchedResultsController 中父实体的列表大小
我正在使用 NSFetchedResultsController,并且在查询父属性中的计数时遇到问题。
假设以下数据模型,'Group', 'Category', 'Item'。
- 项目:所有项目都属于“类别”
- 类别:“类别”可能属于某个“组”
- 组:“组”有零到 N 个“类别”
我想搜索类别中没有任何组的所有项目。
我的代码如下:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((category.groups.@count == 0) || category.categoryId == %@)", categoryId];
...
但是,“category.groups.@count”在这里不起作用。 (它在 NSFetchedRequest 之外运行良好)
我该如何解决这个问题。请帮助我;;
谢谢。
I'm using NSFetchedResultsController and I have a problem to query count in parent attribute.
Assuming that following data model, 'Group', 'Category', 'Item'.
- Item : All items belong to ‘Category’
- Category : ‘Category’ may belong to a certain ‘Group’
- Group : 'Group' has zero to N 'Category'
And I want to search all items which does not have any groups in category.
My codes are following :
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((category.groups.@count == 0) || category.categoryId == %@)", categoryId];
...
But, "category.groups.@count" does not work in here. (It works well out of NSFetchedRequest)
How can I solve this problem. Please help me;;
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关系也可以为零。
当您针对 SQLite 后端运行时,谓词内部代码的解析可能与您直接在 Objective-C 内部期望的不同。当您遇到类似这样的奇怪事情时,打开 sql 调试以查看底层 sql 是什么并相应地调整您的谓词会很有帮助。
A relationship can also be nil.
The resolution of the code inside of the predicate when you are running against a SQLite backend can be different than what you expect inside of Objective-C directly. When you run into odd things like this it can be helpful to turn on sql debugging to see what the underlying sql is and adjust your predicates accordingly.
打开 2 个括号,只关闭一个:
You open 2 brackets, and close only one: