ios - Attempting to save an event in EKEventStore crashes the app -
i have odd bug in app. attempting save event using saveevent
causes app continue in 1 of 3 ways:
- everything gets saved correctly , without issues
- the app crashes
unrecognized selector sent instance
error, offending selectorconstraints:
, object it's sent different , rather unpredictable (they private sdk classes) - the app crashes
exc_bad_access
error
in trying debug this, i've stripped app view controller listing events, button add new one. first time present view controller add event, goes smoothly, second time this, throws error.
here code use:
self.event = ekevent(eventstore: self.eventstore!) self.event!.calendar = self.calendar! self.event!.startdate = self.defaultstartdate() self.event!.enddate = self.event!.startdate.datebyaddingtimeinterval(3600) var error: nserror? self.eventstore!.saveevent(self.event!, span:ekspanthisevent, error: &error) if let e = error { println("saving error: \(error)") }
if values calendar
, startdate
or enddate
invalid, descriptive error no crash, here crashes @ self.eventstore!.saveevent()
. appreciated!
edit turns out due extraneous call self.eventstore.reset()
.
after long search find solution.
have save events in background embedding code on dispatch_async block.
enum uwcalendarerror: int { case alreadyexists case generic case notgranted } class calendar { static func saveevent(title: string, startdate: nsdate, duration: nstimeinterval, completion: (success: bool, error: uwcalendarerror?) -> void) { if calendar.iseventalreadyscheduled(title, startdate: startdate, duration: duration) { completion(success: false, error: .alreadyexists) } else { dispatch_async(dispatch_get_main_queue(),{ let store = ekeventstore() store.requestaccesstoentitytype(ekentitytypeevent) {(granted, error) in if !granted { completion(success: false, error: .notgranted) } var event = ekevent(eventstore: store) event.title = title event.startdate = startdate event.enddate = event.startdate.datebyaddingtimeinterval(duration) event.calendar = store.defaultcalendarfornewevents var err: nserror? store.saveevent(event, span: ekspanthisevent, commit: true, error: &err) if err == nil { completion(success: true, error: nil) } else { completion(success: false, error: .generic) } } }) } } static func iseventalreadyscheduled(title: string, startdate: nsdate, duration: nstimeinterval) -> bool { let enddate = startdate.datebyaddingtimeinterval(duration) let eventstore = ekeventstore() let predicate = eventstore.predicateforeventswithstartdate(startdate, enddate: enddate, calendars: nil) let events = eventstore.eventsmatchingpredicate(predicate) if events == nil { return false } eventtocheck in events { if eventtocheck.title == title { return true } } return false } }
Comments
Post a Comment