如果您想查找超过 4 年的年份,系统会将返回的事件修剪为前 4 年的事件。(请参阅Apple 的文档)。可能的解决方案是批量运行谓词匹配。
这是 Xamarin.iOS 的 C# 扩展
public static class EKEventStoreExtensions
{
private const int MaxPredicateYearSpan = 4;
public static EKEvent[] GetAllEvents(this EKEventStore eventStore, DateTimeOffset startAt, DateTimeOffset endAt, params EKCalendar[] calendars)
{
var isBatched = endAt.Year - startAt.Year >= MaxPredicateYearSpan;
var result = new List<EKEvent>();
var batchStartAt = startAt;
var batchEndAt = endAt;
while (batchStartAt < endAt)
{
if (isBatched)
{
batchEndAt = batchStartAt.AddYears(1);
if (batchEndAt > endAt)
{
batchEndAt = endAt;
}
}
var events = GetEventsMatching(eventStore, batchStartAt, batchEndAt, calendars);
result.AddRange(events);
batchStartAt = batchEndAt;
}
return result.ToArray();
}
private static EKEvent[] GetEventsMatching(EKEventStore eventStore, DateTimeOffset startAt, DateTimeOffset endAt, EKCalendar[] calendars)
{
var startDate = (NSDate)startAt.LocalDateTime;
var endDate = (NSDate)endAt.LocalDateTime;
var fetchCalendarEvents = eventStore.PredicateForEvents(startDate, endDate, calendars);
return eventStore.EventsMatching(fetchCalendarEvents);
}
}