简体   繁体   中英

issue in running method on main thread

I am facing problem in running method on main thread for that i tried GCD NSOperation but nothing is working here. Let me show you code and explain more thing in it,

// 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 
}

What I want is it should not get ahead before this all get completed and then only go move ahead so I can use events array where i need it.

You need to change your idea. The second log with the data is the correct one. When you get the data you should update the UI / use it. You shouldn't be trying to / want to block the main thread to wait for the data to be available.

Your calendarMonthView method can take a block as a parameter which can be called to return the array once it is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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