iPhone - 有什么方法可以取消对 EventKit 数据库的请求吗?

发布于 2024-12-06 17:21:41 字数 467 浏览 3 评论 0原文

我试图弄清楚是否可以取消对 EventKit 数据库的查询。我使用与某一周内所有事件相匹配的谓词来查询 EventKit。此查询可能需要一些时间,具体取决于返回的事件数量。

我为什么要这个?嗯,在我的 iPhone 应用程序中,用户浏览他的日历中的几周。每次向用户显示新的一周时,应用程序都必须加载该周的所有事件。因为这需要几百毫秒,所以应用程序的响应速度没有应有的快。用户基本上必须浏览得慢一点,因为每次他浏览到另一周时,用户界面都会停顿一小会儿。

我已经使用“performSelectorInBackground”在后台线程中运行查询。这仍然使用户界面停滞不前。我正在考虑的解决方案是,每当用户决定浏览到另一周时,当前的 EventKit 查询就会被取消。这样做的缺点可能是用户有时不会立即看到该周的事件,因为查询被取消,但 UI 希望始终保持高度响应。

那么这有可能吗?有谁知道吗?我可以取消 EventKit 查询吗?也许通过停止它正在运行的线程?欢迎任何建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-12-13 17:21:41

目前尚不清楚您如何执行查询。可能使用 -[EKEventStore eventsMatchingPredicate:]?

另一种方法可能是使用 -[EKEventStore enumerateEventsMatchingPredicate:usingBlock:]。对于每个找到的事件,您提供的块都会被调用一次,并且您可以发出信号表明搜索应该停止。执行以下操作:

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

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

          if (shouldStop) {
              *stop = YES;
          }

}];

此调用是同步的,但可以使用dispatch_async 或 NSOperation 在另一个线程上执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文