0

我可以在AppDelegate - application:didReceiveRemoteNotification方法中使用 PromiseKit 吗?

我正在使用 GCM 从服务器推送通知。一旦应用程序收到通知,我想从服务器获取一些数据,解析它,按摩它,然后使用 Core Data 保存它。

当您在 TableViewController 上向下滑动手动刷新数据时,我正在使用 PromiseKit 将这些步骤链接在一起并且工作正常。但是,当我使用相同的逻辑从服务器刷新推送通知的数据时,行为是不可预测的,有时执行停止在firstly(),有时它执行几个then块然后什么也没有。

基本上这就是我会做的:

func application( application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        firstly() {
          // fetch data (JSON) from server
        }.then {
          // Parse the JSON
        }.then {
          // Sync the db (using Core Data)
        }.then {
          // Raise local Notification
        }.error {
        }
}

显然,看起来控件didReceiveRemoteNotification在所有实现之前就存在方法Promises

PromiseKit 大师,有什么建议吗?我唯一能想到的就是在没有 PromiseKit 的情况下重写代码进行顺序操作。

4

1 回答 1

1

PromiseKit 是一个异步库。

您的所有then块都是异步执行的。方法 didReceiveRemoteNotification 将在调用 then 块之前返回。

如果你想进行同步调用,你不应该使用 promisekit!

于 2016-05-16T13:13:31.227 回答