- (void)syncWithCalendar
{
NSMutableDictionary *dictionary = [util readPListData];
NSMutableArray *courses = [util getCourses];
__block NSMutableArray *lessons;
__block NSMutableDictionary *lesson;
NSString *studentID = [util getProgramDetails].studentId;
NSString *programName = [util getProgramDetails].programName;
__block NSString *startDateString = @"", *endDateString = @"";
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSString *currentDateString = [NSString stringWithFormat:@"%@ 09:00:00", [formatter stringFromDate:[NSDate date]]];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSDate *currentDate = [formatter dateFromString:currentDateString];
self.eventstore = [[EKEventStore alloc] init];
[self.eventstore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted) {
NSLog(@"calendar access granted");
for (int i=0; i<[courses count]; i++)
{
lessons = [[courses objectAtIndex:i] objectForKey:@"lessons"];
for (int j=0; j<[lessons count]; j++)
{
lesson = [lessons objectAtIndex:j];
NSString *title = nil;
title = [NSString stringWithFormat:@"%@ %@-Complete %@ lesson",studentID,programName,[lesson objectForKey:@"lesson-name"]];
startDateString = [NSString stringWithFormat:@"%@ %@", [lesson objectForKey:@"exam-date"], @"09:00:00"];
endDateString = [NSString stringWithFormat:@"%@ %@", [lesson objectForKey:@"exam-date"], @"18:00:00"];
if (!([startDateString isEqualToString:@""] && [endDateString isEqualToString:@""]))
{
EKEvent *event = [EKEvent eventWithEventStore:self.eventstore];
event.title=title;
event.startDate = [formatter dateFromString:startDateString];
event.endDate = [formatter dateFromString:endDateString];
event.allDay = NO;
[event setCalendar:[self.eventstore defaultCalendarForNewEvents]];
NSError *err = nil;
[self.eventstore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
//[self.eventstore saveEvent:event span:EKSpanThisEvent error:&err];
if (err) {
NSLog(@"event not saved .. error = %@",err);
} else {
NSLog(@"event added successfully");
}
}
} // lessons for loop
} // courses for loop
} else {
NSLog(@"Access not granted");
}
}];
[self.eventstore release];
[formatter release];
}
成功添加事件后,显示:
“收到内存警告。终止以响应SpringBoard的终止”
并且应用程序终止。这在iOS 7设备中发生。我还没有在iOS 6及以下版本中查看过它。如果只保存了少量事件(30或更少),则代码可以正常工作,但在保存时会显示80个事件或更多事件的内存问题。
答案 0 :(得分:6)
最后我找到了问题的答案。我想分享我的解决方案,以便它可以帮助其他人......
我特意在iOS 7中遇到崩溃,这是因为下面的行
//错了 [self.eventstore saveEvent:event span:EKSpanThisEvent commit:YES error:&amp; err];
保存事件的正确方法:(注意:我的案例不需要事件标识符)
//正确的
[self.eventstore saveEvent:event span:EKSpanThisEvent commit:NO error:&amp; err];
然后使用
[self.eventstore commit:NULL]
保存所有事件后。 这阻止了我的崩溃。
答案 1 :(得分:1)
切换到ARC可以解决内存问题,但是大量数据可能仍会出现问题。 尝试在内部使用@autorelease池进行循环,如例子
for (int i=0; i<[courses count]; i++)
{
@autoreleasepool {
lessons = [[courses objectAtIndex:i] objectForKey:@"lessons"];
for (int j=0; j<[lessons count]; j++)
{
lesson = [lessons objectAtIndex:j];
NSString *title = nil;
title = [NSString stringWithFormat:@"%@ %@-Complete %@ lesson",studentID,programName,[lesson objectForKey:@"lesson-name"]];
startDateString = [NSString stringWithFormat:@"%@ %@", [lesson objectForKey:@"exam-date"], @"09:00:00"];
endDateString = [NSString stringWithFormat:@"%@ %@", [lesson objectForKey:@"exam-date"], @"18:00:00"];
if (!([startDateString isEqualToString:@""] && [endDateString isEqualToString:@""]))
{
EKEvent *event = [EKEvent eventWithEventStore:self.eventstore];
event.title=title;
event.startDate = [formatter dateFromString:startDateString];
event.endDate = [formatter dateFromString:endDateString];
event.allDay = NO;
[event setCalendar:[self.eventstore defaultCalendarForNewEvents]];
NSError *err = nil;
[self.eventstore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
//[self.eventstore saveEvent:event span:EKSpanThisEvent error:&err];
if (err) {
NSLog(@"event not saved .. error = %@",err);
} else {
NSLog(@"event added successfully");
}
}
} // lessons for loop
}//autoreleasepool
} // courses for loop
这样每个for循环步骤都会释放所有自动释放对象。
答案 2 :(得分:0)
@upendar我也有相同的情景,但我被分成小块,I.E。就像你有80个要同步的事件需要20-20并暂停2-5秒。它们之间。
autoreleasepool
很好用,但是Calendar需要太多内存才能执行。一段时间内需要100%以上。
我很喜欢这个问题。真有意思的一个!!!