Search code examples
iosobjective-cekeventekeventstoreekeventkit

EKEventKit - How to retrieve eventIdentifier after batch commit?


I'm doing batch commit after saving a lot of ekEvents. And I need to retrieve the eventIdentifier for each EKEvent. But it's not showing after the commit. I am aware that saving it with

commit:NO 

will not produce eventIdentifier. But surely after issuing a commit the eventIdentifier should be filled up.

Basically my app has to retrieve a bunch of Calendar Event data from a web service and save it on the device. Unfortunately the amount of data being thrown can get up to a 1000 events.

At first I was using this:

[self.ekEventStore saveEvent:ekEvent span:EKSpanFutureEvents commit:YES error:&error];

But the amount of data being thrown to device is causing the EKEventStore to fail with the occasional device restart.

Eventually I realized that it has something to do with the amount of commits I'm throwing at the EventStore and decided to set the commit to NO

[self.ekEventStore saveEvent:ekEvent span:EKSpanFutureEvents commit:NO error:&error];

And at the end of the loop call this:

[self.ekEventStore commit:&error];

But then the issue of eventIdentifier shows up. Even after doing the commit at the end of loop, each event has given me a nil value for the eventIdentifier. This has never happened when I commited each time I save on the eventStore. Am I doing something wrong? I figured that after the commit the events would have registered the eventIdentifier for each instance. But it's not.

Also the issue seems to be prevalent for this fella over here as well.

Thanks


Solution

  • I was finally able to figure out the issue.

    Basically what I needed to do was to do everything inside the:

    - (void)requestAccessToEntityType:(EKEntityType)entityType
                       completion:(EKEventStoreRequestAccessCompletionHandler)completion
    

    method.

    When I said everything, I mean when you are doing a fetch, save, edit, edit and even commi to the EventStore, do it inside the completion block. Provided that the return value of the completion block, granted is set to YES.

    To give context. What I do with the requestAccess method was just request for access and act accordingly with whatever the result is given, whether it's granted or not. Then I do my stuff outside the method.

    It worked for me. If there are people out there who has a different solution. Kindly post it here, for my sake and for anyone else out there who has yet to solve this problem. Thanks.