-2

我的情况是我需要等到方法被执行然后才继续执行我已经尝试过 cfrunlooprun 和

  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);


    dispatch_async(queue, ^{
              [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {

但这对我没有帮助我的代码在下面

- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate 

    toDate:(NSDate*)lastDate{

        [self fatchAllEvent];// this method contain block it takes 1 second to get executed and events array get filled from that block

        NSLog(@"all event %@",events); it shows null here cuse blocked has not executed yet

        [self generateRandomDataForStartDate:startDate endDate:lastDate]; this method need events array but as there is no data so nothing happens 


        return self.dataArray;

    }

我怎么能等到 fatchAllEvent 方法被执行,然后只有在执行处理之后

4

1 回答 1

1
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();

// Add a task to the group
dispatch_group_async(group, queue, ^{
    [self method1:a];
});

// Add another task to the group
dispatch_group_async(group, queue, ^{
    [self method2:a];
});

// Add a handler function for when the entire group completes
// It's possible that this will happen immediately if the other methods have already finished
dispatch_group_notify(group, queue, ^{
     [self methodFinish]
});

Dispatch groups are ARC managed. They are retained by the system until all of their blocks run, so their memory management is easy under ARC.

See also dispatch_group_wait() if you want to block execution until the group finishes.
于 2013-08-16T07:44:12.880 回答