I have two entities:
AgendaEvents
AgendaDates
AgendaDates has a many to many relationship with AgendaEvents.
I am trying to store in a temporary array (var myTempEvents = [AgendaEvent] ()) all the AgendaEvents which have inside AgendaDates a date (at least) which is equal to a defined date (myDate)
myDate is a date selected from a calendar (JTAppleCalendar).
for date in calendar.selectedDates {
myDate = date
}
now I need to get all the AgendaEvent object which have in agendaDates at least a date equals to myDate. I need the time NOT to affect the comparison.
I have a function to use for this:
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
}
in my core data the relation between AgendaEvents and AgendaDate is agendaDates and AgendaDates has an object dates (of type Date).
Now i can perform the fetch and I'll do it in this func:
func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) {
myTempEvents = try! context.fetch(agendaEventsWithDate(date: selectedDate))
let myEvent = myTempEvent[indexPath.row]
}
Here myTemEvents array should be filled with all the events which in AgendaDates have at least on date equals to myDate.
My issue now are: 1) myDate and the date in the predicate have time component. The time in myDate it depends on when the user select the date. The one in AgendaDate is store in core data. This means that normally the are never the same and so myTempEvents will always be empty.+
2) Since i cannot perform the comparison because of problem number one. I don't know if the fetch request works.
Thank you!
UPDATE------- WOrking Copy
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()
}