0

我在主线程上运行方法时遇到问题,因为我尝试了 GCD NSOperation 但这里没有任何工作。让我向您展示代码并解释其中的更多内容,

// method which get called first 
- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView
                 marksFromDate:(NSDate*)startDate
                        toDate:(NSDate*)lastDate
{
  [self fatchAllEvent];
    // prints null here it generate all event array which i need to use in below method 
  NSLog(@"all event %@",events); 
  [self generateRandomDataForStartDate:startDate endDate:lastDate];
  return self.dataArray;
}

-(void)fatchAllEvent
{
  eventStore = [[EKEventStore alloc] init];
  if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){
    __block typeof (self) weakSelf = self; 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
      if(granted){
        NSLog(@"granted");
        [weakSelf performSelectorOnMainThread:@selector(allCalendarEvent)
         withObject:nil
         waitUntilDone:YES];
                //You can see that i have performed on main thread and wait until done
      }
      else{
        NSLog(@"Not granted");
      }
    }];
  }
  else{
    [self allCalendarEvent];
  }
}

-(void)allCalendarEvent
{
  NSDate *startDate = [NSDate distantPast];
  NSDate *endDate = [NSDate distantFuture];
  NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];
  NSDate* currentStart = [NSDate dateWithTimeInterval:0
                                            sinceDate:startDate];
  int seconds_in_year = 60*60*24*365;
  while([currentStart compare:endDate] == NSOrderedAscending){
    NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year
                                               sinceDate:currentStart];
    if([currentFinish compare:endDate] == NSOrderedDescending){
      currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:endDate];
    }
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:currentStart
                                                                 endDate:currentFinish
                                                               calendars:nil];
    [eventStore enumerateEventsMatchingPredicate:predicate
                                      usingBlock:^(EKEvent *event, BOOL *stop) {
                                        if(event){
                                          [eventsDict setObject:event
                                                         forKey:event.eventIdentifier];
                                        }
                                      }];
    currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1)
                                      sinceDate:currentStart];
  }
  events = [eventsDict allValues];
  NSLog(@"all event %@",events);   // this gets print after some seconds of the method where i need is already executed 
}

我想要的是在这一切都完成之前它不应该提前,然后才继续前进,这样我就可以在需要的地方使用事件数组。

4

1 回答 1

0

你需要改变你的想法。包含数据的第二个日志是正确的。当您获得数据时,您应该更新 UI / 使用它。您不应该试图/想要阻止主线程等待数据可用。

您的calendarMonthView方法可以将块作为参数,一旦可用,就可以调用该块以返回数组。

于 2013-08-13T06:40:59.863 回答