将事件添加到自定义日历会使应用程序崩溃
Add Event to custom calendar crashed the application
我正在尝试将活动添加到自定义日历。
获取我在下面使用的日历列表
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(!granted) {
//error alert
return;
}
NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
// List of all calendars
// Let user to choose a calendar.
}];
为了添加事件,我使用了以下代码
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
现在,当我使用默认日历时,它已成功添加,但当我选择任何其他日历时,应用程序崩溃并在日志中显示
*** -[EKSource retain]: message sent to deallocated instance 0x7f9bddb1bf50
我无法找出确切的问题或出现此问题的线路。任何建议都将不胜感激。
您必须检查该特定日历是否允许您修改它
if ([storeObject calendar].allowsContentModifications)
{
//add event now
}
是的。您的商店对象在完成块开始到 运行.
之前被释放
最简单的修复方法 - 将商店对象保存为字段:
EKEventStore *store;
-(void)addEvent:(EventStoreClass *)storeObject
{
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
此问题已通过执行以下操作解决。
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
if([storeObject calendar]) {
NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
__block EKCalendar *calendar;
[calendars enumerateObjectsUsingBlock:^(EKCalendar *cal, NSUInteger idx, BOOL *stop) {
if([[cal title] isEqualToString:[[storeObject calendar] title]]) {
calendar = cal;
*stop = YES;
}
}];
[event setCalendar:calendar];
}
else {
[event setCalendar:[store defaultCalendarForNewEvents]];
}
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
问题出在 [storeObject calendar]
,因为这个日历是从另一家商店拿来的,我们正在为它分配不同的商店。所以我只是从同一家商店拿了日历,而且很管用。
我正在尝试将活动添加到自定义日历。
获取我在下面使用的日历列表
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(!granted) {
//error alert
return;
}
NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
// List of all calendars
// Let user to choose a calendar.
}];
为了添加事件,我使用了以下代码
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
现在,当我使用默认日历时,它已成功添加,但当我选择任何其他日历时,应用程序崩溃并在日志中显示
*** -[EKSource retain]: message sent to deallocated instance 0x7f9bddb1bf50
我无法找出确切的问题或出现此问题的线路。任何建议都将不胜感激。
您必须检查该特定日历是否允许您修改它
if ([storeObject calendar].allowsContentModifications)
{
//add event now
}
是的。您的商店对象在完成块开始到 运行.
之前被释放最简单的修复方法 - 将商店对象保存为字段:
EKEventStore *store;
-(void)addEvent:(EventStoreClass *)storeObject
{
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
此问题已通过执行以下操作解决。
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
if([storeObject calendar]) {
NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
__block EKCalendar *calendar;
[calendars enumerateObjectsUsingBlock:^(EKCalendar *cal, NSUInteger idx, BOOL *stop) {
if([[cal title] isEqualToString:[[storeObject calendar] title]]) {
calendar = cal;
*stop = YES;
}
}];
[event setCalendar:calendar];
}
else {
[event setCalendar:[store defaultCalendarForNewEvents]];
}
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
问题出在 [storeObject calendar]
,因为这个日历是从另一家商店拿来的,我们正在为它分配不同的商店。所以我只是从同一家商店拿了日历,而且很管用。