简体   繁体   English

如何检索已存在1个月以上的EKEvent?

[英]How do I retrieve an EKEvent that is greater than 1 month old?

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 找到:1个活动
used:2016-12-15 05:00:00 +0000 使用时间:2016-12-15 05:00:00 +0000
2016-12-16 05:00:00 +0000 2016-12-16 05:00:00 +0000

going back earlier results in 0 events. 返回较早的结果为0个事件。 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-11 05:00:00 +0000
2016-11-12 05:00:00 +0000 2016-11-12 05:00:00 +0000
found 0 items using iterator 使用迭代器找到0个项目

Is it possible to retrieve events from, say 2014? 是否有可能从2014年检索事件? I see them when using the calendar app (macOS and iOS). 使用日历应用程序(macOS和iOS)时,我会看到它们。

I think what happened is that the settings on my device changed during the iOS upgrade process (I know not how). 我认为发生的事情是我的设备上的设置在iOS升级过程中发生了变化(我不知道如何)。 In Settings/Calendar You can specify how events are synched. 在“设置/日历”中,您可以指定事件的同步方式。 I found it was set to "1 month". 我发现它被设置为“ 1个月”。 I am now able to retrieve events older than 1 month. 现在,我可以检索1个月以上的事件。

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. 在您的示例中,您只是告诉它要在1天的范围内进行搜索。 You're also narrowing it down to one specific calendar (you can pass nil to search all calendars). 您还可以将其范围缩小到一个特定的日历(可以通过nil来搜索所有日历)。 If you want events from 2014, then give it a range in 2014. 如果您想要2014年的活动,请在2014年提供范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM