我正在使用 EventKit API 来获取用户的提醒。fetch API 是异步完成的,当 fetch 完成时,您可以使用一个完成块。但是,在我获取用户的提醒后,我将它分配给 self.eventKitReminderEvents 实例变量(我已经调试并看到它不是零)。然后在我调用一个块之后,但从这个块内部 self.eventKitReminderEvents 是nil。我真的不明白为什么会这样。也许有人可以看看我的代码并给我一个线索。
我正在使用这样的 API:
- (void)futureReminders {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[self fetchReminderEventsInBackgroundFromDate:[NSDate date] withCompletionBlock:^{
// When I enter here self.eventKitReminderEvents is nil
// Which is weird because it should be set already if you look in
// fetchReminderEventsInBackgroundFromDate below...
[self processEventKitRemindersWithCompletion:^(NSArray *reminderEvents) {
// Save a copy in memory
self.futureReminderEvents = reminderEvents;
// Send a signal that indicates that this asynchronous task is completed ...
dispatch_semaphore_signal(sema);
}];
}];
// Wait for dispatch signal
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
- (void)fetchReminderEventsInBackgroundFromDate:(NSDate *)startDate withCompletionBlock:(void(^)(void))block {
DLogName()
//NSDate *startDate = [NSDate date];
NSDate *endDate = [NSDate distantFuture]; // This will get me events 4 years from now
// Create the predicate
NSPredicate *predicate = [self.eventStore predicateForIncompleteRemindersWithDueDateStarting:startDate ending:endDate calendars:nil];
[self.eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
self.eventKitReminderEvents = reminders;
block();
}];
}