停止线程执行,直到API异步线程完成

时间:2013-07-13 22:10:09

标签: iphone objective-c multithreading grand-central-dispatch reminders

我目前正在浏览一系列日期并缓存与每个日期相关联的EKEventsEKReminders。我遇到的问题是,当我获取EKReminder时,API会异步运行。问题是我需要从当前的调度线程队列中访问该线程的内容。我创建了一个局部变量并__block'd它,以便我可以将API async的内容分配给它,然后我将整个fetch调用包装在dispatch_sync调用中,希望它能保住我当前的队列,直到async为止。完成但不会发生。在异步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;
    });

}

1 个答案:

答案 0 :(得分:0)

dispatch_sync()阻止后的代码移至fetchRemindersMatchingPredicate:的完成阻止。在异步任务完成之前等待做某事正是完成Block的目的。

此外,不要对当前队列进行dispatch_sync()次调用。保证串行队列死锁,即使队列并发也很有可能。