2017-06-20 26 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。我解決這個問題的方式是重置事件存儲。一旦我這樣做了,日曆創建正確,爲提醒和事件,事件和提醒都被添加到他們各自的日曆。