title: Swift. Event not saving to calendar
body: <p>I'm trying to copy specific events from all calendars to the target calendar. Unfortunately my events are not saving in target calendar.</p>
  
  <p>Simply my code in steps:</p>
  
  <ul>
  <li>Check permissions (success) </li>
  <li>Load calendars    (success)</li>
  <li>Load events       (success)</li>
  <li>Save events       (failed)</li>
  </ul>
  
  <p>I'm sure there are events to save from terminal which prints "Trying to save" couple of times.</p>
  
  <p>And it looks like code pass through "try self.eventStore.save(event, span: .thisEvent)" and exits function there without calling "Saved" or entering catch clause.</p>
  
  <p>There is a source code:</p>
  
  <pre><code>import UIKit
  import EventKit
  
  class ViewController: UIViewController{
  
      @IBOutlet weak var status: UILabel!
  
      var calendars: [EKCalendar]?
  
      var targetCalendar: EKCalendar?
      var targetCalendarEvents: [EKEvent]?
  
      let eventStore = EKEventStore()
  
      let targetCalendarName = "TargetCalendarName"
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func viewWillAppear(_ animated: Bool) {
          DispatchQueue.main.async{
              self.status.text = "Idle"
          }
  
          checkCalendarAuthorizationStatus()
      }
  
      func checkCalendarAuthorizationStatus() {
          let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
  
          switch (status) {
          case EKAuthorizationStatus.notDetermined:
              // This happens on first-run
              requestAccessToCalendar()
          case EKAuthorizationStatus.authorized:
              // Things are in line with being able to show the calendars in the table view
              loadCalendars()
              loadEvents()
          case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
              // We need to help them give us permission
              print("Missing permissions [00]")
          }
      }
  
      func requestAccessToCalendar() {
          eventStore.requestAccess(to: EKEntityType.event, completion: {
              (accessGranted: Bool, error: Error?) in
  
              if accessGranted == true {
                  DispatchQueue.main.async(execute: {
                      self.loadCalendars()
                      self.loadEvents()
                  })
              } else {
                  print("Missing permissions [01]")
              }
          })
      }
  
      func loadEvents(){
          print("Loading..")
          DispatchQueue.main.async{
              self.status.text = "Loading"
          }
  
          let eventStore = EKEventStore()
  
          var initialized = false
  
          //Two months
          let dateTo = NSDate(timeIntervalSinceNow: +30*24*3600 * 2)
  
          for calendar in self.calendars! {
              let predicate = eventStore.predicateForEvents(withStart: Date() as Date as Date, end: dateTo as Date, calendars: [calendar])
  
              let events = eventStore.events(matching: predicate)
  
              if calendar.title == targetCalendarName  {
                  print("Initialized")
                  targetCalendar = calendar
                  targetCalendarEvents = events
                  initialized = true
              }
          }
  
          if(!initialized){
              print("Not Initialized")
          } else {
              for calendar in self.calendars! {
  
                  let predicate = eventStore.predicateForEvents(withStart: Date() as Date as Date, end: dateTo as Date, calendars: [calendar])
  
                  let events = eventStore.events(matching: predicate)
  
                  if calendar.title != targetCalendarName &amp;&amp; calendar.title != "Contacts" {
  
                      //print("Loaded Calendar \(calendar.title)")
                      print("Loaded Calendar")
  
                      for event in events {
                          if(!(event.location?.isEmpty)!){
                              //print("Event \(event.location ?? "Missing Location") \(event.startDate) \(event.endDate)")
  
                              addEventToTarget(eventToAdd: event)
                          }
                      }
                  }
              }
          }
  
  
          DispatchQueue.main.async {
              self.status.text = "Done"
          }
          print("Done")
      }
  
      func loadCalendars() {
          self.calendars = eventStore.calendars(for: EKEntityType.event)
      }
  
      func addEventToTarget(eventToAdd: EKEvent){
  
          eventStore.requestAccess(to: .event) { (granted, error) in
  
              for event in self.targetCalendarEvents! {
                  if(!(event.location?.isEmpty)!){
                      if(
                          eventToAdd.title == event.title &amp;&amp;
                          eventToAdd.startDate == event.startDate &amp;&amp;
                          eventToAdd.endDate == event.endDate
                      ){
                          print("Possible duplicate - skipping")
                          return
                      }
                  }
              }
  
              if (granted) &amp;&amp; (error == nil) {
  
                  let event:EKEvent = EKEvent(eventStore: self.eventStore)
  
                  event.title = eventToAdd.title
                  event.startDate = eventToAdd.startDate
                  event.endDate = eventToAdd.endDate
                  event.notes = ""
                  event.location = eventToAdd.location
  
                  event.calendar = self.targetCalendar
  
                  //print("Trying to save \(event.title) \(String(describing: event.location))")
                  print("Trying to save")
                  do {
                      try self.eventStore.save(event, span: .thisEvent)
                      print("Saved \(event.title) \(String(describing: event.location)) in \(event.calendar.title)")
                  } catch {
                      print("failed to save event with error : \(error as NSError)")
                  }
              }
              else{
  
                  print("failed to save event with error : \(String(describing: error)) or access not granted")
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>TargetCalendarName is correct calendar name</strong></p>
  
  <hr>
  
  <p>Sometimes there are events which saves successfully but only couple of them (2-5) for hundreds which should save. I don't have a clue why.
  So 5 for 200 is not enough for me.</p>