【问题标题】:How to retrieve events older than 3 years old from EKEventStore如何从 EKEventStore 检索超过 3 年的事件
【发布时间】:2018-08-25 21:01:03
【问题描述】:

我刚刚运行了以下代码(在我的 iPhone 上):

  let predicate = eventStore.predicateForEvents(withStart: Date(timeIntervalSinceNow: -60*60*24*365*4), end: Date(timeIntervalSinceNow: 60*60*24*30), calendars: [calendar])

结果显示,这个请求只能追溯到 3 年前:

找到的最早开始日期:2015-04-11 12:00:00 +0000

有没有办法检索超过三年的日历事件?我想找回我以前的课堂笔记。当我在 Mac 上使用日历时,它会列出每个日历的计数,但我无法检索较旧的对象。

【问题讨论】:

    标签: ios swift eventkit ekeventstore


    【解决方案1】:

    ** 更新答案 **

    我对此进行了深入研究,发现了following from Apple,这解释了为什么 OP 会出现此问题:

    出于性能原因,此方法仅匹配其中的那些事件 四年的时间跨度。如果 startDate 和 endDate 之间的日期范围 大于四年,则缩短为前四年。

    话虽如此,您可以使用以下代码作为示例创建四年增量:

    if let fourYearsAgo = Calendar.current.date(byAdding: .year, value: -4, to: Date()) {
        let predicate = eventStore.predicateForEvents(withStart: fourYearsAgo,
                                                      end: Date(),
                                                      calendars: [calendar])
    }
    

    【讨论】:

    • 谢谢!当我缩小谓词的事件时间跨度时,我也遇到了较旧的事件。我想我会尝试不同的方法,直到我满意我拥有大部分/所有我期望的数据。
    • @Mozahler 更新了来自 Apple 的信息,解释了为什么会发生这种情况。
    • 但我只返回 3 个 - 而不是请求的 4 个。当然,我的查询跨越了 4 年零一个月。无论如何,感谢您的努力。很高兴您抽出时间回复我。
    • Apple 的日常恐怖——有很多选择导致这甚至成为性能问题。另一个限制是人们希望 Apple 将这个选择留给开发人员,也许还有潜在性能问题的警告。感谢您指出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2012-07-16
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多