0

using this code:

    func sampleCase() {
    var dateConstructor = DateComponents()
    dateConstructor.year = 2016
    dateConstructor.month = 12
    dateConstructor.day = 15
    let calendar: Calendar = Calendar.current
    let workCalendar = CalendarUtilities.calendar("WorkSchedule", using: eventStore)
    let referenceDate = calendar.date(from: dateConstructor)!
        let startDate = referenceDate
        let endDate = referenceDate.addingDays(1)  // midnight
        let range = DateRange(start: startDate, andEnd: endDate)
    let predicate = eventStore.predicateForEvents(withStart: range.startDate, end:range.endDate, calendars:[workCalendar])
    let events = eventStore.events(matching: predicate)
    print("Found: \(events.count) events")
    print("used:\(startDate)")
    print("\(endDate)")

}

I only get correct results for events in the previous month... (These examples use dates that have events and should return non-zero values.)

authorization was granted...
Found: 1 events
used:2016-12-15 05:00:00 +0000
2016-12-16 05:00:00 +0000

going back earlier results in 0 events. I also tried using enumeration with similar results:

        eventStore.enumerateEvents(matching: predicate) { (result, stop) in
        counter += 1
    }
    print("found \(counter) items using iterator")

used:2016-11-11 05:00:00 +0000
2016-11-12 05:00:00 +0000
found 0 items using iterator

Is it possible to retrieve events from, say 2014? I see them when using the calendar app (macOS and iOS).

Mozahler
  • 4,958
  • 6
  • 36
  • 56

2 Answers2

2

I think what happened is that the settings on my device changed during the iOS upgrade process (I know not how). In Settings/Calendar You can specify how events are synched. I found it was set to "1 month". I am now able to retrieve events older than 1 month.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
0

The event store should return whatever matches the predicate you give it as long as you don't make the range too large (there IS a limit, I'd have to go look it up). In your example, you're only telling it to search within a range of 1 day. You're also narrowing it down to one specific calendar (you can pass nil to search all calendars). If you want events from 2014, then give it a range in 2014.

ghostatron
  • 2,620
  • 23
  • 27
  • A larger range comes up empty (you used to be able to get about 4 years worth of data, now it seems you can only get one month), so I made it very small, on a day and in a calendar that I know has EKEvents for that day. – Mozahler Jan 13 '17 at 14:00
  • @Mozahler Curious, I do remember hitting a range limit with the predicate, and I honestly feel like it was lower than 4 years...but I have an app in the app store that currently pulls a 6 month range (today +/- 3 months), and it pulls in everything. Your predicate looks well formed, only difference is I don't specify a calendar. I don't see where you make the store, but that should be trivial anyway unless it's nil for some reason? – ghostatron Jan 13 '17 at 22:11
  • Interesting. I queried for 4 months ago to 2 months ago and got 6 instead of 100s... I'll look to see what they were. Perhaps it is a cloud issue? I'll look into alternate ways of querying the store as well. Thanks for the input! – Mozahler Jan 14 '17 at 18:33