开发者

iPhone - Is there any way to cancel a request to the EventKit database?

I am trying to figure out if it is possible to cancel a query to the EventKit database. I query the EventKit with a predicate that matches all events in a certain week. This query can take some time depending on how many events are returned.

Why do I want this? Well, in my iPhone app the user browses through the weeks of his calendar. Every time a new week is shown to the user, the app has to load all the events for that week. Because this takes a couple of hundred milliseconds, the app isn't as responsive as it should be. The user basically has to browse a little slower because the user interface stalls for a short while every time 开发者_开发百科he browses to another week.

I am already running the query in a background thread using "performSelectorInBackground". This still stalls the user interface quite a bit. The solution that I'm thinking of is that whenever the user decides to browse to another week, the current EventKit query is cancelled. The drawback of this is probably that the user sometimes doesn't see the events for that week immediately because the query was cancelled, but the UI will hopefully be highly responsive at all times.

So is this at all possible? Does anyone know? Can I cancel an EventKit query? Maybe by stopping the thread it is running in? Any advice is welcome!


It's not clear how you are performing the query at the moment. Possibly using -[EKEventStore eventsMatchingPredicate:]?

An alternative might be to use -[EKEventStore enumerateEventsMatchingPredicate:usingBlock:]. The block you supply is called once for each event found and you can signal that the search should stop. Do something like:

[eventStore enumerateEventsMatchingPredicate:predicate
       usingBlock:^(EKEvent *event, BOOL *stop) {

          if (event) {
              ...add event to some collection...
          }

          if (shouldStop) {
              *stop = YES;
          }

}];

This call is synchronous but can be performed on another thread with dispatch_async or NSOperation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜