0

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()
}
Marco
  • 1,051
  • 1
  • 17
  • 40
  • Did you try it with the predicate I suggested in https://stackoverflow.com/a/46218672/1187415? You compute startDate/endDate, but don't use them. – Martin R Sep 14 '17 at 15:07
  • I tried but i didn't understand how to use myDate in the predicate u have suggested – Marco Sep 14 '17 at 15:12
  • Just `fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@).@count > 0", startOfDay as NSDate, endOfDay as NSDate)` – Martin R Sep 14 '17 at 15:13
  • $a.dates..... dates is the object in the AgendaDate right? how i compare this with the selectedDate (myDate)? – Marco Sep 14 '17 at 15:15
  • Just keep your code as it is, only replace the `fetchRequest.predicate = ...` line with the one above, using the SUBQUERY. Please try it! – Martin R Sep 14 '17 at 15:17
  • @Marco is the time component of the date needed? or do you just need the date. – Just a coder Sep 14 '17 at 15:20
  • @MartinR i've update the question to show you. I've done as you asked. myTempEvent return nil – Marco Sep 14 '17 at 15:22
  • @iOSCalendarViewOnMyProfile no i don't need the time just the date... patchthecode right? :) – Marco Sep 14 '17 at 15:23
  • What does `try! context.fetch(...)` return? – Martin R Sep 14 '17 at 15:25
  • @Marco yes, its me. One thing, if you do not need the time component of the date, then why not compare them as strings using a DateFormatter(). You can set the date format as "yyyy MM dd". That way you can represent the dates as a simple string eg "2017 01 22". This way there is no time component to worry about. – Just a coder Sep 14 '17 at 15:25
  • @iOSCalendarViewOnMyProfile: This is about a Core Data fetch request. The dates are stored as *numbers* (seconds since Jan 1, 2001) in the SQLite database. – Martin R Sep 14 '17 at 15:27
  • @iOSCalendarViewOnMyProfile that's an option...but in core date is stored as date...I should change it and store as a string ( or also as a string in a new object)! I hoped there was another solution...but if it's not I'll do so... – Marco Sep 14 '17 at 15:28
  • @MartinR it returns nil – Marco Sep 14 '17 at 15:29
  • How can that be? A forced try does not return an optional. – Martin R Sep 14 '17 at 15:34
  • @MartinR honestly i don't know... i guess my mistake again... if i print myTempEvent after myTempEvent = try! context.... then myTempEvent it's nitl... probably that not the fetchrequest though,.. – Marco Sep 14 '17 at 15:58
  • @MartinR if i print directly the fetch ( print(try! context.fetch(agendaEventsWithDate(date: selectedDate))) ) it finally gives the result i was looking for!!! i don't understand why it doesn't store it inside myTempEvents though... – Marco Sep 14 '17 at 16:14
  • I am not sure how yet.... but finally i got it working!!! thanks to you'all guys! special thanks to @MartinR which with infinite patience helped me all through the process (basically all the afternoon!) I do really appreciate it! thx! i will update the question to show the final result. – Marco Sep 14 '17 at 16:33

0 Answers0