Core Data比较coredata对象与给定date的date

我有两个实体:

AgendaEvents

AgendaDates

AgendaDates与AgendaEvents有多对多的关系。

我试图存储在一个临时数组(var myTempEvents = [AgendaEvent]())所有AgendaDates内部AgendaDatesdate(至less)等于一个定义的date(myDate)

myDate是从日历(JTAppleCalendar)中select的date。

for date in calendar.selectedDates { myDate = date } 

现在我需要获得议程date中至less有一个date等于myDate的所有AgendaEvent对象。 我需要时间不影响比较。

我有一个函数来使用这个:

  func agendaEventsWithDate(date: Date) -> NSFetchRequest<AgendaEvent> { // create a fetch request that will retrieve all the AgendaEvents. let fetchRequest = NSFetchRequest<AgendaEvent>(entityName: "AgendaEvent") // set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date. let cal = Calendar.current let startOfDay = cal.startOfDay(for: date)print(selectedDate) let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)! fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.agendaDates >= %@ AND $a.agendaDates < %@).@count > 0", startOfDay as NSDate, endOfDay as NSDate) return fetchRequest } 

在我的核心数据中,AgendaEvents和AgendaDate之间的关系是议程date,议程date有一个对象date(Datetypes)。

现在我可以执行获取,我会在这个function:

  func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) { myTempEvents = try! context.fetch(agendaEventsWithDate(date: selectedDate)) let myEvent = myTempEvent[indexPath.row] } 

这里myTemEvents数组应该填充在AgendaDates至less在date等于myDate的所有事件。

我现在的问题是:1)myDate和谓词中的date有时间组件。 myDate中的时间取决于用户何时selectdate。 AgendaDate中的一个存储在核心数据中。 这意味着通常情况下永远不会相同,所以myTempEvents将永远是空的

2)由于第一个问题,我不能进行比较。 我不知道取回请求是否工作。

谢谢!

更新——-复制

  func agendaEventsWithDate(date: Date) -> NSFetchRequest<AgendaEvent> { // create a fetch request that will retrieve all the AgendaEvents. let fetchRequest = NSFetchRequest<AgendaEvent>(entityName: "AgendaEvent") // set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date. let cal = Calendar.current let startOfDay = cal.startOfDay(for: date) print(selectedDate) let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)! fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.agendaDates >= %@ AND $a.agendaDates < %@).@count > 0", startOfDay as NSDate, endOfDay as NSDate) return fetchRequest } func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) { let myEvent = myTempEvents[indexPath.row] cell.configureCell(agendaEvent: myEvent) } func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) { selectedDate = date calendar.reloadData() myTempEvents = try! context.fetch(agendaEventsWithDate(date: selectedDate)) tableView.reloadData() }