简体   繁体   English

在核心数据中合并三个实体?

[英]Union three entities in Core Data?

I have the following Entities: 我有以下实体:

Student
  NSString name
  Team inTeam // reverse of Team.students

Team
  NSSet assignments
  NSOrderedSet students // reverse relationship of Student.inTeam

Assignment
  NSDate date // timeless date, only care about the day
  NSNumber assignmentCount
  Team inTeam // reverse of Team.assignmets

No all Students are part of a Team. 没有所有学生都属于团队。 Not all teams have Assignments. 并非所有团队都有任务。 Teams are usually formed by 2 Students (no more than 3). 小组通常由2个学生组成(不超过3个)。

For all the Teams, I need to display the name of its members and the assignmentCount for a given date. 对于所有团队,我需要显示给定日期的成员名称和assignmentCount。

I can think of different ways to do this, 3 of them: 我可以想到执行此操作的不同方法,其中3种:

  1. Get all the teams, for each team query Assignment with the given date. 获取所有团队,为每个团队查询具有给定日期的Assignment。 This is not efficient (but almost the same as if I were getting everything in a single query having assignmentCount as a fault object?) 这效率不高(但是几乎就像我在将assignmentCount作为故障对象的单个查询中获取所有内容一样?)

  2. Keep a collection of all the Teams and keep a collection of all the Assignments with the given date. 保留所有团队的集合,并保留给定日期的所有任务的集合。 Only two queries, but need to keep the second collection in sync with the db (the assignmentCount will change, and would rather not do this) 仅两个查询,但是需要使第二个集合与db保持同步(assignmentCount将更改,并且宁愿不这样做)

  3. Reload the data every time the assignmentCount changes. 每次AssignmentCount更改时都重新加载数据。 Obviously not a good solution 显然不是一个好的解决方案

Is there a way to get all this information in a single query? 有没有办法在单个查询中获取所有这些信息?

A problem I see is that if I set my predicate to have something like ANY team.assignments.date = %@ it only returns the teams that have assignments. 我看到的一个问题是,如果我将谓词设置为具有ANY team.assignments.date = %@之类的东西,它只会返回具有任务的团队。

Any ideas? 有任何想法吗?

Use a fetched results controller (or at least a fetch request that pages in the results). 使用获取的结果控制器(或至少在页面中分页的获取请求)。 The FRC will manage direct changes on the Team objects and efficient loading from the store when scrolling the list. 当滚动列表时,FRC将管理Team对象的直接更改以及从商店的有效加载。

You can configure the fetch request to prefetch information from the relationships that you know you will always need. 您可以将获取请求配置为从您将永远需要的关系中预取信息。 You should use the relationships to get that information, not additional fetch requests. 您应该使用关系来获取该信息,而不是其他获取请求。

For assignmentCount for a given date , that is a good candidate for a new fetch. 对于assignmentCount for a given date ,这是进行新提取的良好选择。 You don't actually need the assignments so use countForFetchRequest:error: and setting a predicate to limit to team == ... . 实际上,您实际上不需要分配,因此请使用countForFetchRequest:error:并设置谓词以限制为team == ...

This is very efficient: 这非常有效:

1) Fetch the teams with the appropriate predicate. 1)使用适当的谓词获取团队。

[NSPredicate predicateWithFormat:
   @"ANY assignments.date > %@ && assigments.date < %@", 
   dateStartTime, dateEndTime];

2) Access the data like this: 2)像这样访问数据:

NSOrderedSet *students = team.students;
NSUInteger assignments = team.assignments.count;

That is the reason for Core Data. 这就是使用核心数据的原因。 It will do all the optimizations for you in the background. 它将在后台为您进行所有优化。 If you use a NSFetchedResultsController it can notify your table view of changes, all without any effort on your part. 如果您使用NSFetchedResultsController它可以通知您的表视图更改,而无需您费力。

And yes, you are right that the fetch would not include teams without assignments. 是的,您说对了,提取不会包含没有分配任务的团队,这是正确的。 This would be intended behavior if you want to filter by date. 如果要按日期过滤,这将是预期的行为。

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

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