主线程上运行方法的问题

时间:2013-08-13 06:14:33

标签: iphone ios

我在主线程上运行方法遇到问题,我试过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 
}

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

1 个答案:

答案 0 :(得分:0)

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

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