我目前正在遍历一系列日期并缓存与每个日期关联的EKEvents
和EKReminders
。我遇到的问题是,当我获取 时EKReminder
,API 异步运行。问题是我需要从我当前的调度线程队列中访问该线程的内容。我创建了一个局部变量并 __block'd 以便我可以将 API 异步的内容分配给它,然后我将整个 fetch 调用包装在一个 dispatch_sync 调用中,希望它能够保持我当前的队列直到异步是完成,但不会发生。如何在异步 API 调用完成之前停止当前线程?
我在代码下面提供了两条注释,向您展示我需要停止代码执行并等待的位置。
- (void)setupCalendarCache {
// Dispatch the queueing of previous days to a background thread
dispatch_queue_t previousDaysCacheQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(previousDaysCacheQueue, ^{
NSLog(@"Preparing to cache previous calendar days.");
int reverseYearsToCache = (HZ_NUMBER_OF_TOTAL_YEARS_TO_CACHE * 365) / 2;
DEDateUtility *dateUtility = [[DEDateUtility alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
dispatch_apply(reverseYearsToCache, previousDaysCacheQueue, ^(size_t i) {
@synchronized (self.datesOnCalendar) {
NSLog(@"synchronizing calendar dates.");
// Setup start and end time for event & reminder fetching.
NSDate *date = [dateUtility adjustDate:self.currentDate byNumberOfDays:i-1];
NSLog(@"Working date %@", [date description]);
[self.datesOnCalendar insertObject:date atIndex:0];
// Start of the day
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.hour = 0;
dateComponents.minute = 0;
dateComponents.second = 0;
NSDate *startDate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
// End of the day
dateComponents.hour = 23;
dateComponents.minute = 59;
dateComponents.second = 59;
NSDate *endDate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
// Collections for the reminders and events fetched.
NSArray *events = [[NSArray alloc] init];
// Must be blocked so that the Block thread can access it even after this method is completed.
__block __strong NSArray *remindersList = [[NSArray alloc] init];
NSLog(@"Building predicates.");
// Predicates for fetching the reminders and events.
NSPredicate *fetchPredicateForEvents = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:[self.eventStore calendarsForEntityType:EKEntityTypeEvent]];
NSPredicate *fetchPredicateForReminders = [self.eventStore predicateForIncompleteRemindersWithDueDateStarting:startDate ending:endDate calendars:[self.eventStore calendarsForEntityType:EKEntityTypeReminder]];
// Fetch the events matching the aforementioned NSPredicate.
events = [self.eventStore eventsMatchingPredicate:fetchPredicateForEvents];
// Don't store a nil array in the dictionary.
if (!events) {
events = [[NSArray alloc] init];
NSLog(@"No events found for this day.");
} else {
NSLog(@"Found %d events.", [events count]);
}
// ISSUE: Need to block execution until the following dispatch_sync is completed. How can I do this?
NSLog(@"Running reminders block.");
dispatch_sync(previousDaysCacheQueue, ^ {
// Fetch the reminders matching the aforementioned NSPredicate.
[self.eventStore fetchRemindersMatchingPredicate:fetchPredicateForReminders completion:^(NSArray *reminders) {
if (reminders ) {
remindersList = reminders;
NSLog(@"Found %d reminders (%d)", [remindersList count], [reminders count]);
return;
}
}];
});
NSLog(@"Building dictionaries for events and reminders.");
// CANT DO THIS. API async invocation is still taking place.
//Place the events and reminders in the dictionary
NSDictionary *eventsAndRemindersForDay = @{@"events": events, @"reminders" : remindersList};
[previousEventsAndReminders setObject:eventsAndRemindersForDay forKey:[date description]];
for (int index = 0; index < [remindersList count]; index++) {
EKReminder *reminder = remindersList[index];
NSLog(@"reminder: %@", reminder.title);
}
}
});
self.cachingStage += 1;
});
}