1

Currently, I am using an API (PowerAPI) in which the "authenticate" function is called and then once the user's data has been processed, a notification is sent out. This authenticate function needs to be called as a background fetch. My question is whether my current way of calling the completion handler is even calling the completion handler and if there is a better way?

Currently this is in my app delegate class:

let api = PowerAPI.sharedInstance
var completionHandler: ((UIBackgroundFetchResult) -> Void)? = nil
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("BG fetch start")
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTranscript:", name:"transcript_parsed", object: nil)
    self.completionHandler = completionHandler
    api.authenticate("server.com", username: "User", password: "password", fetchTranscript: true)
}
func handleTranscript(notification : NSNotification){
    print("BG fetch finit")
    completionHandler!(UIBackgroundFetchResult.NewData)
    print(api.studentInformation)
}

The API is a singleton type object.

EDIT: The PowerAPI object is a class I wrote to download student data from a server and parse it. The "transcript_parsed" notification is a notification generated from within the PowerAPI directly after the "transcript_fetched" notification is sent out in the following asynchronous code (Also within PowerAPI):

 let task = session.dataTaskWithRequest(request) {
        (let data, let response, let error) in
        guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
            print("error")
            return
        }
        switch notificationID {
        case "authentication_finished":
            //NSString(data: data!, encoding: NSUTF8StringEncoding)! //used to return data from authentication
            let success = self.parse(data!) //notification object is true if success
            NSNotificationCenter.defaultCenter().postNotificationName(notificationID, object: success)
        case "transcript_fetched":
            NSNotificationCenter.defaultCenter().postNotificationName(notificationID, object: data)
        default:
            break
        }
    }
user3274871
  • 194
  • 1
  • 1
  • 11
  • 1
    You need to provide more information about what you're doing and the tools that you're using. Where is a "transcript_parsed" notification generated? And what is `PowerAPI`? – Duncan C Mar 27 '16 at 13:22
  • Sorry, additional information provided in edit now. – user3274871 Mar 27 '16 at 14:41
  • 1
    Have you added breakpoints to see if your posts are called or if it fell into your guard statement? – Dare Mar 27 '16 at 14:46
  • Yes, everything works correctly. I just don't know how to check whether the completion handler is being called. Also I'm not sure as to whether the way it is working is the "recommended" way of doing things. – user3274871 Mar 27 '16 at 14:52

0 Answers0