繁体   English   中英

主线程上运行方法的问题

[英]issue in running method on main thread

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

我想要的是它不应该在所有步骤完成之前就取得成功,然后才继续前进,这样我就可以在需要的地方使用事件数组。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM