0

I am trying to run a NSURLSessionDataTask in my app delegate on a background fetch but although I get valid data from the completion handler the app isn't firing the method. Here is my code

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSURLSession *serverConfigure = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSessionConfiguration *serverSession = [NSURLSession sessionWithConfiguration:serverConfigure delegate:self delegateQueue:nil];

    NSURLSessionDataTask *serverDownload = [serverSession dataTaskWithURL:[NSURL URLWithString:@"myurl"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSArray *downloadedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        [self handleData:downloadedData];

    }];

    [serverDownload resume];

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Joe Barbour
  • 842
  • 9
  • 14
  • 1
    Your completion handler won't be running on the main thread (which is where you'll need to do UI updates.) – Matt Gibson Nov 28 '14 at 10:35
  • Thanks for your reply but I have tried placing the method in ' dispatch_async(dispatch_get_main_queue()' but still no response – Joe Barbour Nov 28 '14 at 12:02
  • We may need a bit of clarification, then. When you say you get valid data from the completion handler, which of the two completion handlers are you talking about? And when you say "the app isn't firing the method", which method are you talking about? Your "handleData"? – Matt Gibson Nov 28 '14 at 16:36
  • Yes, sorry for being un clear there. So the NSArray returns data. However the [self handleData:downloadedData]; method doesn't fire even though I've placed it in a main queue block. Could the problem because I'm doing this in the app delegate? Does the main queue run if the app is in the background? Im guessing not. – Joe Barbour Nov 28 '14 at 17:39

1 Answers1

0

In backgroundFetch your application will call this method in background -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

for this you have to activate background fetch mode ...just go through this link http://www.appcoda.com/ios7-background-fetch-programming/

Bevan
  • 342
  • 2
  • 14
  • Thanks for your reply. Background fetch is working the method its self is called when I push a silent notification and thus it runs the NSURLSession and returns the data in the debugger. The problem it is is it doesn't call the method '[self handleData:downloadedData];' – Joe Barbour Nov 28 '14 at 11:59
  • 1
    When u get notification....by clicking on notification ur "application did received remote notification.." method will call...Check ur download Data task with url method...in that check Whether u returning a completion block...i.e completiton(data,response,error); this will solve u r problem – Bevan Nov 28 '14 at 12:35