imme
imme

Reputation: 319

Swift - didReceiveRemoteNotification - PromiseKit

Can I use PromiseKit in AppDelegate - application:didReceiveRemoteNotification method?

I am using GCM to push notification from the server. Once the app receives the notification I would like to fetch some data from the server, parse it, massage it and then save it using Core Data.

Am using PromiseKit to chain the steps together and its working fine when you manually refresh the data with a swipe down on TableViewController. However, when I use the same logic to refresh data on push notification from the sever, the behaviour is unpredictable, sometimes the execution stops at firstly(), other times it executes couple of then blocks and then nothing.

Basically this is what I would do:

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 {
        }
}

Obviously, looks like the control exists the didReceiveRemoteNotification method before all the Promises are fulfilled.

PromiseKit gurus, any suggestions? The only thing I can think of is to rewrite the code without PromiseKit for sequential operation.

Upvotes: 0

Views: 277

Answers (1)

João Nunes
João Nunes

Reputation: 3761

PromiseKit is an asynch library.

All your then blocks are executed asynchronously. The method didReceiveRemoteNotification will return before your then blocks are called.

If you want to make a synch call, you should not use promisekit!

Upvotes: 1

Related Questions