Pergunta

So I need to get the current event in the calendar. I.E - an event that started and did not end yet. I have written some code but it does not work. Through debugging I noticed my oneDayAgo variable is nil and I do not understand why. The oneWeekFromNow variable is good.

Here is the method I have written:

-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
                      completion:^(BOOL granted, NSError *error) {
                          NSCalendar *calendar = [NSCalendar currentCalendar];
                          NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
                          oneDayAgoComponents.day -=1;
                          NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                                        toDate:[NSDate date]
                                                                       options:0];
                          NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
                          oneWeekFromNowComponents.week = 1;
                          NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
                                                                             toDate:[NSDate date]
                                                                            options:0];
                          NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
                                                                                       endDate:oneWeekFromNow
                                                                                     calendars:nil];

                          NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
                          // Fetch all events that match the predicate
                          [self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
                              if (([event.startDate compare:[NSDate date]] == NSOrderedDescending) &&
                                  ([[NSDate date] compare:event.endDate] == NSOrderedDescending)) {
                                  [currentEvens addObject:event];
                              }

                          }];


                          self.lblEvent.text = [NSString stringWithFormat:@"%@", currentEvens];
                          [self.view reloadInputViews];

                      }];

}

Foi útil?

Solução

Try this instead:

NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;

Outras dicas

Here is the corrected code that worked for me. I also needed to modify some other things:

-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
                      completion:^(BOOL granted, NSError *error) {
                          NSCalendar *calendar = [NSCalendar currentCalendar];
                          NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
                          oneDayAgoComponents.day = -1;
                          NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                                        toDate:[NSDate date]
                                                                       options:0];
                          NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
                          oneWeekFromNowComponents.week = 1;
                          NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
                                                                             toDate:[NSDate date]
                                                                            options:0];
                          NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
                                                                                       endDate:oneWeekFromNow
                                                                                     calendars:nil];

                          NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
                          // Fetch all events that match the predicate
                          [self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
                              if (([event.startDate compare:[NSDate date]] == NSOrderedAscending) &&
                                  ([[NSDate date] compare:event.endDate] == NSOrderedAscending)) {
                                  [currentEvens addObject:event];
                              }

                          }];

                          NSString *currentEventsString = [[NSString alloc]init];
                          for (EKEvent *event in currentEvens) {
                              currentEventsString = [currentEventsString stringByAppendingString:event.title];
                          }

                          dispatch_async(dispatch_get_main_queue(), ^{
                              self.lblEvent.text = currentEventsString;
                          });

                      }];

}

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top