2016-11-18 61 views
1

我試圖做一個日曆本地或在Gmail(日曆選擇用戶,我在下面的描述answer)使用事件巨大的負荷加載散裝事件得到錯誤

添加一個事件與我下面提到的功能對我來說工作正常,但是當我不得不做大量的例如527事件(因爲我試圖添加學生的學校日曆)它不起作用正確。

當做大量加載時,我插入了大約100個事件,然後它開始崩潰並崩潰應用程序。

的誤差,它給我有以下幾種:

2016年11月17日17:23:35.966 [230:11481]日曆未設置:1個錯誤 域= EKErrorDomain代碼= 1「沒有選擇日曆。「 UserInfo = {NSLocalizedDescription =未選擇日曆}

2016-11-17 17:23:49.545 [230:12644]連接中斷!

二○一六年十一月一十七日17:23:49.568 [230:12587]錯誤得到改變對象ID 由於從守護程序時間戳501092601.149441:錯誤 域= NSMachErrorDomain代碼= 4097 「未知錯誤代碼」

我的問題是這樣的:在我的大容量加載方法或我已經完成的功能中是否有任何錯誤?或其他有沒有其他更好的方法來完成大量的事件?

該功能可以插入事件的列表:

- (int) addCalendarEvents: (EKCalendar *) cal { 

    int num = 0; 

    for (int i=0; i < [calendario.eventos count]; i++) { 

     NSDictionary * nextDict = [calendario.eventos objectAtIndex:i]; 
     Evento_DTO * evento_dto = [[Evento_DTO alloc] initWithEventos:nextDict]; 

     BOOL res = [self addEventCalendar: evento_dto calendar: cal]; 

     if(res){ 
      num++; 
     } 
    } 

    return num; 
} 

這將活動添加到日曆的功能如下:

-(BOOL)addEventCalendar: (Evento_DTO *) evento calendar: (EKCalendar *) cal{ 

    __block BOOL res = NO; 

    if (!SYSTEM_VERSION_LESS_THAN(@"6.0")) { 
     // iOS 6 and later 

     EKEventStore *eventStore = [[EKEventStore alloc] init]; 

     //We get the dates of the event 
     Fecha_DTO *fechaStart = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtStart]; 
     Fecha_DTO *fechaEnd = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtEnd]; 

     // Format the dates to type NSDate 
     // Start Date 
     NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
     [df setDateFormat:@"yyyyMMdd'T'HHmmss"]; 

     if (fechaStart.tzid == nil) { 
      [df setTimeZone: [NSTimeZone systemTimeZone]]; 
     }else{ 
      [df setTimeZone:[NSTimeZone timeZoneWithName:fechaStart.tzid]]; 
     } 
     NSDate* parsedDateS = [df dateFromString: fechaStart.fecha]; 

     // End Date 
     NSDateFormatter* df2 = [[NSDateFormatter alloc] init]; 
     [df2 setDateFormat:@"yyyyMMdd'T'HHmmss"]; 

     if (fechaEnd.tzid == nil) { 
      [df2 setTimeZone: [NSTimeZone systemTimeZone]]; 
     }else{ 
      [df2 setTimeZone:[NSTimeZone timeZoneWithName:fechaEnd.tzid]]; 
     } 
     NSDate* parsedDateE = [df2 dateFromString: fechaEnd.fecha]; 

     //rRules 
     NSString *rfc2445String = evento.rRule; // Usando la libreria EKRecurrenceRule+RRULE.m 
     EKRecurrenceRule *recurrenceRule; 
     if (![rfc2445String isEqualToString:@""]) { 
      recurrenceRule = [[EKRecurrenceRule alloc] initWithString:rfc2445String andTimezone:fechaStart.tzid]; 
      // NSLog(@"RRule: %@", recurrenceRule); 
     } 

     if(parsedDateS!=nil){ 

      [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 

       if (granted) { 
        EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
        event.title  = evento.summary; 
        event.notes  = evento.description; 
        event.startDate = parsedDateS; 
        event.endDate = parsedDateE; 
        event.location = evento.location; 

        if (![rfc2445String isEqualToString:@""]) 
        event.recurrenceRules = [NSArray arrayWithObject:recurrenceRule]; 


        event.calendar = [eventStore calendarWithIdentifier: cal.calendarIdentifier]; 

        //[event setCalendar:[eventStore defaultCalendarForNewEvents]]; 
        NSError *err = nil; 

        BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; 

        if(!success){ 
         if (err) { 
          NSLog(@"Calendar was not set: %li %@", (long)err.code, err.description); 
         } 
        }else{ 

         //NSLog(@"Added Event"); 
         res = YES; 
        } 

       } else { 
        // code here for when the user does NOT allow your app to access the calendar 
        alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"") 
                 message:AMLocalizedString(@"errorPermisosCal", @"") 
                 delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil, nil]; 
        [alerta show]; 
       } 
      }]; 
     }else{ 
      NSLog(@"The start date is null"); 
     } 

     df = nil; 
     df2 = nil; 
    }else{ 

     alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"") 
              message:AMLocalizedString(@"VersionEvento", @"") 
              delegate:self 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil, nil]; 
     [alerta show]; 
    } 
    return res; 
} 
+0

我們最近在大型事件集中發現了這種現象 - 有3個事件沒有eventIdentifiers。這對我們的代碼造成了一個問題,但是我們通過檢查零並理解對應用程序的影響來解決問題。 –

+0

@AndyWeinstein我終於可以做到大規模無大錯,看看我的答案 – Joacer

回答

0

最後,我已經能夠進行大規模的大部分事件沒有失敗,我修改的方法如下:

- (void) addCalendarEvents: (EKCalendar *) cal store: (EKEventStore *) eventStore { 

    if (!SYSTEM_VERSION_LESS_THAN(@"6.0")) { 
     // iOS 6 and later 

     [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 

      if (granted) { 
       //se añade cada uno de los eventos 
       for (int i=0; i < [calendario.eventos count]; i++) { 
        @autoreleasepool { 
         NSDictionary * nextDict = [calendario.eventos objectAtIndex:i]; 
         Evento_DTO * evento_dto = [[Evento_DTO alloc] initWithEventos:nextDict]; 

         [self addEventCalendar: evento_dto calendar: cal.calendarIdentifier store: eventStore]; 
        } 
       } 
      } else { 
       // code here for when the user does NOT allow your app to access the calendar 
       alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"") 
                message:AMLocalizedString(@"errorPermisosCal", @"") 
                delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil, nil]; 
       [alerta show]; 
      } 
     }]; 
    }else{ 

     alerta = [[UIAlertView alloc]initWithTitle:AMLocalizedString(@"Error", @"") 
              message:AMLocalizedString(@"Event version", @"") 
              delegate:self 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil, nil]; 
     [alerta show]; 
    } 
} 

這將活動添加到日曆功能如下:

-(void)addEventCalendar: (Evento_DTO *) evento calendar: (NSString *) cal store: (EKEventStore *) eventStore{ 

    //Obtenemos las fechas del evento 
    Fecha_DTO *fechaStart = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtStart]; 
    Fecha_DTO *fechaEnd = [[Fecha_DTO alloc] initWithFecha:(NSDictionary *)evento.dtEnd]; 

    // Format the dates to type NSDate 
    // Start Date 
    NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"yyyyMMdd'T'HHmmss"]; 

    if (fechaStart.tzid == nil) { 
     [df setTimeZone: [NSTimeZone systemTimeZone]]; 
    }else{ 
     [df setTimeZone:[NSTimeZone timeZoneWithName:fechaStart.tzid]]; 
    } 
    NSDate* parsedDateS = [df dateFromString: fechaStart.fecha]; 

    // End Date 
    NSDateFormatter* df2 = [[NSDateFormatter alloc] init]; 
    [df2 setDateFormat:@"yyyyMMdd'T'HHmmss"]; 

    if (fechaEnd.tzid == nil) { 
     [df2 setTimeZone: [NSTimeZone systemTimeZone]]; 
    }else{ 
     [df2 setTimeZone:[NSTimeZone timeZoneWithName:fechaEnd.tzid]]; 
    } 
    NSDate* parsedDateE = [df2 dateFromString: fechaEnd.fecha]; 

    //rRules 
    NSString *rfc2445String = evento.rRule; 
    EKRecurrenceRule *recurrenceRule; 

    if (![rfc2445String isEqualToString:@""]) { 
     recurrenceRule = [[EKRecurrenceRule alloc] initWithString:rfc2445String andTimezone:fechaStart.tzid]; 
     //NSLog(@"RRule: %@", recurrenceRule); 
    } 

    if(parsedDateS!=nil){ 

     EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
     event.title  = evento.summary; 
     event.notes  = evento.description; 
     event.location = evento.location; 
     event.startDate = parsedDateS; 
     event.endDate = parsedDateE; 

     if (![rfc2445String isEqualToString:@""]) 
      event.recurrenceRules = [NSArray arrayWithObject:recurrenceRule]; 

     event.calendar = [eventStore calendarWithIdentifier: cal]; 

     NSError *err = nil; 

     BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; 

     if(!success){ 
      if (err) { 
       NSLog(@"Calendar was not set: %li %@", (long)err.code, err.description); 
      } 
     }else{ 

      NSLog(@"Added Event"); 
     } 
    }else{ 
     NSLog(@"The start date is null"); 
    } 

    df = nil; 
    df2 = nil; 
} 

我希望它可以幫助別人。