简体   繁体   English

从关系中获取请求CoreData

[英]Fetch request CoreData from relationship

This is the situation, I have two entities in CoreData. 这种情况,我在CoreData中有两个实体。

  • Question (Chapter:int, Answers:array) 问题(第章:int,答案:数组)
  • Answer (Date, Correct:bool) 答案(日期,正确:布尔)

And this is how they are related: 它们是如何关联的:

  • The question entity has the chapter where it belongs and a history of answers to that question (that's a relationship). 问题实体具有其所属章节和该问题的答案历史记录(即关系)。
  • The answer entity knows when the question was answered and wether it was correct or not. 答案实体知道问题何时被回答以及它是否正确。
  • One question has many answers. 一个问题有很多答案。

The final result I want is something like this... 我想要的最终结果是这样的...

Fetch questions WHERE chapter = <some> and (LAST) answer.correct = YES

I'm kind of new with core data and right now I'm doing it with many steps but I wanted to know if I can achieve this with only one NSFetchedResultsController without filtering arrays. 我对核心数据有点陌生,现在我正在执行很多步骤,但是我想知道是否可以仅使用一个NSFetchedResultsController而不过滤数组来实现。

Thank you very much for your help! 非常感谢您的帮助!

Try this predicate. 试试这个谓词。 May work for you. 可能为您工作。

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"chapter = '%@' AND answer[LAST].correct=TRUE",chapterName];

A little convoluted, but I think the following will get what you want: 有点令人费解,但我认为以下将满足您的要求:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"chapter like %@ AND (SUBQUERY(answer, $t, $t.correct == YES).date.@max = SUBQUERY(answer, $t, $t.correct IN {YES, NO}).date.@max)"];

This checks whether the maximum date for correct answers is equal to the maximum date for all answers. 这将检查正确答案的最大日期是否等于所有答案的最大日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM