【问题标题】:Fetching reminders in background using EventKit使用 EventKit 在后台获取提醒
【发布时间】:2015-05-31 18:49:20
【问题描述】:

我正在创建一个 Apple Watch 应用,它有一个按钮可以调用父 iOS 应用。

WKInterfaceController.openParentApplication(message, reply: {
            (response: [NSObject:AnyObject]!, error: NSError!) in
    // processing data from iOS app
})

iOS 应用通过使用 EventKit 检索一些提醒来做出响应,如下所示:

eventStore.fetchRemindersMatchingPredicate(predicate, completion: {
                [unowned self] reminders in
    // sending back data as reply to Apply Watch
})

当父应用程序在前台时,一切都像魅力一样工作。但是当我让应用程序进入后台时,会发生一些奇怪的事情。对 EKEventStore 的调用,eventStore.fetchRemindersMatchingPredicate(predicate),从不调用我的完成块。所以我从来没有因此而向 Apple Watch 发送回复?

所以出了什么问题?我怀疑 EventKit 中存在错误,或者可能不支持在后台调用 EventKit。任何人都可以对此有所了解吗?

【问题讨论】:

    标签: ios watchkit eventkit


    【解决方案1】:

    我想通了!在让EKEventStore 产生异步进程之前,我必须启动一个后台任务。当我完成异步调用时,我必须明确结束后台任务。

    // First let iOS know you're starting a background task
    let taskIdentifier = application.beginBackgroundTaskWithExpirationHandler() {
        () -> Void in
        // Do something when task is taking too long
    }
    // Then do the async call to EKEventStore
    eventStore.fetchRemindersMatchingPredicate(predicate, completion: {
                [unowned self] reminders in
        // Do what I have to do, and afterwards end the background task:
        application.endBackgroundTask(taskIdentifier)
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多