Methods returning nill values

45 views Asked by At

The below method is return the nil values, I have observed that when the method is runnning in the block the method is returning the values with out excuting.

It is first returning the _cards and then the nlogs are printing.

This setter method and this how can i use the completion hadler here

Please help how to resolve this . Thaks in Advance

- (NSMutableArray *)cards
    {
        if (_cards) return _cards;
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
            _cards = [[NSMutableArray alloc] init];
            [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
                /* This code will run when uses has made his/her choice */

                NSString * myString = @"2014-01-01";
                NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
                dateFormatter.dateFormat = @"yyyy-MM-dd";
                NSDate *startDate = [dateFormatter dateFromString:myString];

                NSDate *endDate = [[NSDate alloc] init];

                //   NSArray *calendars = [eventStore calendars];
                NSArray *calendars = [eventStore calendarsForEntityType:EKCalendarChooserDisplayAllCalendars];
                NSLog(@"Calendars %@",calendars);
                NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
                                                                             endDate:endDate calendars:calendars];
                matchingEvents = [eventStore eventsMatchingPredicate:predicate];
                NSIndexPath * indexpath;
                NSString * event = [[matchingEvents objectAtIndex:indexpath.row] valueForKey:@"title"];
                EKEvent *anEvent;
                NSLog(@"Total Events >> %lu",(unsigned long)[matchingEvents count]);

                for (int j=0; j < [ matchingEvents count]; j++) {
                    [_cards addObject:@(ANDYCardStateNormal)];

                    anEvent = [matchingEvents objectAtIndex:j];
                    NSLog(@"Title >>>%@",anEvent.title);
                    NSLog(@"start date is %@ \n End Date is >>> %@",anEvent.startDate,anEvent.endDate);

                }
            }];
        }

        return _cards;
    }
0

There are 0 answers