Akshara
Akshara

Reputation: 141

NSUrlConnection not called via background fetch

 -(void)application:(UIApplication )application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

 NSLog(@"In performFetchWithCompletionHandler");
 DownloadAlerts alertDownload = [[DownloadAlerts alloc]init]; 
[alertDownload getAlertsOnLocUpdate]; 
completionHandler(UIBackgroundFetchResultNewData); // We will add content here soon. 
}

[NSURLConnection sendAsynchronousRequest:request
                                       queue:[[NSOperationQueue alloc] init]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data,
                                               NSError *error) {

                               if ([data length] >0 && error == nil) {
                                   NSLog(@"Hello01");

                               } else if ([data length] == 0 && error == nil) {
                                   NSLog(@"Hello02");

                               } else if (error != nil) {
                                   NSLog(@"Hello03");
                               }
                           }];

I am executing the above piece of code in a function that gets called via the performFetchWithCompletionHandler in appdelegate.h. When I launch the app via background fetch, none of the 3 if blocks get executed. How do I implement this? I need to download data via background fetch. Kindly help... NSUrlSession also doens't work..

Upvotes: 1

Views: 214

Answers (2)

Artem Aleksandrov
Artem Aleksandrov

Reputation: 352

Need to call "completionHandler(UIBackgroundFetchResultNewData);" after you received the data, pass it into "getAlertsOnLocUpdate" method as a parameter, and call it after you receive the answer from server (in completionHandler of URLConnection)

Upvotes: 0

NixSolutionsMobile
NixSolutionsMobile

Reputation: 191

Have you tried to send synchronous request? I think your`s performFetchWithCompletionHandler method finishes before NSURLConnection receives the response. by the way, are you sure that performFetchWithCompletionHandler is called? Maybe you need to set capability for it?

Upvotes: 1

Related Questions