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
}
私が望むのは、これがすべて完了する前に先に進むべきではなく、必要な場所でイベント配列を使用できるように先に進むことだけです。