如何使用获取结果控制器获取的对象列表添加其他对象? [英] How to add additional objects with fetch result controller fetched objects list?

查看:71
本文介绍了如何使用获取结果控制器获取的对象列表添加其他对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Event的实体,它具有s开始和结束日期属性。我必须通过在UI中按日期对事件进行分组来显示事件列表。我正在使用 NSFetchedResultsController 来获取并列出事件。

I have an entity called "Event" which has s start and end date properties. I have to show the list of events by grouping them by date in UI. I am using NSFetchedResultsController to fetch and list the events.

让我们假设一个事件的开始日期为今天,结束日期为明天,我需要在用户界面的两个不同日期显示此事件,但数据库中只有一个条目。

Let's assume an event has start date today and end date tomorrow, here I need to show this event on two different dates in UI but I will have only one entry in the database.

我真的不想为一个事件创建多个条目,而且我希望使用提取结果控制器,因为它减少了很多人工计算。有什么办法解决这个问题?

I really don’t want to create multiple entries for an event and also I wish to use fetch result controller as it reduces lot manual calculation. Is there any way to solve this?

推荐答案

NSFetchedResultsController 很好地匹配了 NSManagedObjects 到tableView的indexPaths。但是,映射是一对一的,因此任何给定的事件只能在indexPaths中出现一次。要实现所需的功能,您需要创建一个新实体,例如 EventDate ,并具有一个 Event EventDate 。定义一个字符串属性,该属性反映日期是在事件的开始还是结束。然后,将FRC基于 EventDate 实体。尽管每个EventDate仅会出现一次,但多个EventDate可能与一个Event有关。从EventDate到Event的逆关系可以使您从多个EventDate访问单个事件的详细信息。

NSFetchedResultsController does a good job of matching NSManagedObjects to indexPaths for a tableView. However, the mapping is one to one, so any given Event can only appear once in the indexPaths. To achieve what you want, you would need to create a new entity, say EventDate, with a to-many relationship from Event to EventDate. Define a string attribute which reflects whether the date is at the start or end of the event. Then base your FRC on the EventDate entity. Although each EventDate will appear only once, multiple EventDates could relate to a single Event. The inverse relationship from EventDate to Event will enable you to access the details of a single Event from multiple EventDates.

这篇关于如何使用获取结果控制器获取的对象列表添加其他对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆