2017-06-20 46 views
0

我有一个问题,提醒日历或事件日历,取决于哪个显示弹出的第一个,创建多个日历,并没有保存任何事件/提醒。我有在plist中设置的属性。下面是我使用要求对事件和提醒接入代码:Xamarin iOS EventStore.Getcalendars返回0

  var calendarEvent = await ANApi.FetchCalendarInfoAsync(AppCore.CurrentAppInfo.AppId); 
      calendarEvent.Count(); 

      foreach (var calEvent in calendarEvent.ToList()) 
      { 
       if (calEvent.Type == 1) 
       { 
        AppEvent.Current.EventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError err) => 
         { 
          if (granted) 
          { 
           lock (_lock) 
           { 
            CreateCalendar(EKEntityType.Event); 
            CreateEvent(calEvent); 
           } 
          } 

         }); 
       } 
       else 
       { 
        AppEvent.Current.EventStore.RequestAccess(EKEntityType.Reminder, (bool remGranted, NSError remErr) => 
        { 
         if (remGranted) 
         { 
          lock (_lock) 
          { 
           CreateCalendar(EKEntityType.Reminder); 
           CreateReminder(calEvent); 
          } 
         } 
        }); 
       } 
      } 

的代码工作正常,如果我只接收事件或提醒,但因为我收到的事件和提醒,它未能正确创建我的日历。

这是我正在创建日历:

public void CreateCalendar(EKEntityType type) 
    { 
     bool calExists = false; 
     var appName = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleDisplayName"); 

     //This returns 0 calendars, depending on which event's request was displayed first. 
     EKCalendar[] calendars = AppEvent.Current.EventStore.GetCalendars(type); 
     foreach (EKCalendar cal in calendars) 
     { 
      if (type == EKEntityType.Event) 
      { 
       if (PersistentLayer.Instance.GetString(Constant.CALENDAR_ID) == null) 
        calExists = false; 
       else if (cal.CalendarIdentifier == PersistentLayer.Instance.GetString(Constant.CALENDAR_ID)) 
        calExists = true; 
      } 
      else 
      { 
       if (PersistentLayer.Instance.GetString(Constant.REMINDER_CALENDAR_ID) == null) 
        calExists = false; 
       else if (cal.CalendarIdentifier == PersistentLayer.Instance.GetString(Constant.REMINDER_CALENDAR_ID)) 
        calExists = true; 
      } 
     } 
     //Create a Calendar based on the App's name. If name cannot be found use App Calendar 
     if (!calExists) 
     { 
      EKCalendar calendar = EKCalendar.Create(type, AppEvent.Current.EventStore); 

      if (appName != null) 
       calendar.Title = appName.ToString(); 
      else 
       calendar.Title = "App"; 
      EKSource localSource = null; 

      foreach (EKSource source in AppEvent.Current.EventStore.Sources) 
      { 
       if (source.SourceType == EKSourceType.CalDav) 
       { 
        localSource = source; 
        break; 
       } 
      } 

      if (localSource == null) 
       return; 

      calendar.Source = localSource; 
      calendar.CGColor = new CGColor(255, 255, 0); 
      NSError calError; 

      AppEvent.Current.EventStore.SaveCalendar(calendar, true, out calError); 
      if (calError != null) 
      { 
       this.SimpleAlert("Error saving Calender", calError.ToString(), "OK", null); 
       return; 
      } 

      //Store the calendar Id so we can use it when saving events 
      if (type == EKEntityType.Event) 
       PersistentLayer.Instance.Edit().PutString(Constant.CALENDAR_ID, calendar.CalendarIdentifier); 
      else 
       PersistentLayer.Instance.Edit().PutString(Constant.REMINDER_CALENDAR_ID, calendar.CalendarIdentifier); 
     } 
    } 

我相信有某种竞争条件怎么回事,但我一直没能弄明白。我尝试了很多事情来尝试并使其运行,但我没有取得任何成功。

谢谢

回答

0

我最终弄明白了。首先,我打破了代码并创建了所有收到的提醒,然后是事件。

起初,当尝试创建事件时,这给了我一个错误域= EKCADErrorDomain代码= 1010。我解决这个问题的方式是重置事件存储。一旦我这样做了,日历创建正确,为提醒和事件,事件和提醒都被添加到他们各自的日历。