简体   繁体   English

在iOS日历中加载批量事件获取错误

[英]Loading bulk event in ios calendar get error

I am trying to do a massive load of events on a calendar either local or in gmail (the calendar chooses the user as I describe in the following answer ) using . 我正在尝试使用在本地或gmail的日历上进行大量的事件(日历选择用户,如我在以下答案中所述 )。

Adding an event with the functions I've put below works fine for me, but when I have to do a massive load of eg 527 events (since I'm trying to add a student's school calendar) it does not work correctly. 使用我在下面提供的功能添加事件对我来说很好用,但是当我必须进行大量例如527个事件的负载(因为我正试图添加学生的学校日历)时,它无法正常工作。

When doing the bulk load I inserted well about 100 events or so and then it starts to crash and crash the app. 当进行批量加载时,我插入了大约100个左右的事件,然后它开始崩溃并使应用程序崩溃。

The errors that it gives me are the following: 它给我的错误如下:

2016-11-17 17:23:35.966 [230:11481] Calendar was not set: 1 Error Domain=EKErrorDomain Code=1 "No calendar selected." 2016-11-17 17:23:35.966 [230:11481]未设置日历:1错误域= EKErrorDomain代码= 1“未选择日历”。 UserInfo={NSLocalizedDescription=No calendar selected.} UserInfo = {NSLocalizedDescription =未选择日历。}

2016-11-17 17:23:49.545 [230:12644] Connectioninterrupted! 2016-11-17 17:23:49.545 [230:12644]连接中断!

2016-11-17 17:23:49.568[230:12587] Error getting changed object IDs since timestamp 501092601.149441 from daemon: Error Domain=NSMachErrorDomain Code=4097 "unknown error code" 2016-11-17 17:23:49.568 [230:12587]从守护程序获取时间戳为501092601.149441以来的更改的对象ID时出错:错误域= NSMachErrorDomain代码= 4097“未知错误代码”

My question is this: Is there any error in my massive loading approach or in the functions I have done? 我的问题是: 我的大量加载方法或完成的功能是否有错误? or else Is there any other better way to do a massive load of events? 还是有其他更好的方法来处理大量事件?

Function that inserts the list of events: 插入事件列表的函数:

- (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;
}

And the function that adds the event to the calendar is as follows: 并将事件添加到日历的函数如下:

-(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;
}

Finally I have been able to perform the massive bulk of events without failures, I have modified the methods being as follows: 最终,我能够执行大量事件而不会失败,我修改了如下方法:

- (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];
    }
}

And the function that adds the event to the calendar is as follows: 并将事件添加到日历的函数如下:

-(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;
}

I hope it helps somebody. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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