PFQuery findObjectsInBackgroundWithBlock:在 WatchKit 应用程序在模拟器中工作,除非 iOS 应用程序在前台,否则不能在手表上工作

PFQuery findObjectsInBackgroundWithBlock: in WatchKit app works in simulator, not on watch unless iOS app is in foreground

我正在编写一个 WatchKit 应用程序,它需要对我们的 Parse 服务器进行查询。因为这是一个网络调用,所以我遵循以下过程:

  1. 我使用 openParentApplication:reply:
  2. 从 WatchKit 调用 iOS 应用程序
  3. handleWatchKitExtensionRequest:userInfo:reply: 内部,我使用 UIBackgroundTaskIdentifier 启动后台任务以确保 iOS 应用程序不会立即终止
  4. 我设置 PFQuery 并调用 findObjectsInBackgroundWithBlock:
  5. 在我传给PFQuery的block中,我回复相关信息给watch

我大概在 Apple Watch 推出前的一个月前编写了这个应用程序,它 运行 在 Watch 模拟器中运行良好。昨天我收到了我的 Apple Watch,我可以在手表上安装应用程序 运行,但它不太好用 - 我设法弄清楚当 iOS 应用程序处于活动状态时 运行在前台就iPhone,那么一切都很好;但是如果应用程序未处于活动状态(例如后台运行或终止),那么 findObjectsInBackgroundWithBlock: 永远不会真正调用该块,并且执行似乎只是停止。

有人知道会发生什么吗?我最初的想法是,iOS 应用程序在完成查询之前正在获取 suspended/killed,但我很确定后台任务会阻止它。

PS。如果有人因为正在寻找如何在实际手表上获得 WatchKit 应用程序 运行 而偶然发现这个问题,我发现 this post 对它的工作非常有帮助。

原来我有几个与 openParentApplication:reply: 相关的问题,它们似乎都已通过使用此 post:

中的技术得到解决

http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

我已经为我自己的应用程序启动了后台任务,但我没有使用此 post 中描述的 "bogus workaround" 后台任务,因为在模拟器中我不需要它.不过,在手表上,它显然是。