在ios中应用程序处于后台时如何从服务器下载数据

在ios中应用程序处于后台时如何从服务器下载数据,ios,xcode6,apple-push-notifications,background-fetch,Ios,Xcode6,Apple Push Notifications,Background Fetch,在我的应用程序中,在收到推送通知后,当应用程序处于后台时,我需要从服务器下载数据并保存到数据库中。这是我的密码 NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; NSURLRequest *request = [NSURLRequest requestWithURL:Url]; [NSURLConnection sendAsynchronousRequest:request

在我的应用程序中,在收到推送通知后,当应用程序处于后台时,我需要从服务器下载数据并保存到数据库中。这是我的密码

   NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
   NSURLRequest *request = [NSURLRequest requestWithURL:Url];
   [NSURLConnection sendAsynchronousRequest:request
                                   queue:myQueue
                       completionHandler:^(NSURLResponse *response, 
   NSData *data, NSError *error){
     [self saveDataIntoDB:data];      
    }];
此代码仅在应用程序位于前台时执行。 谢谢。

尝试直接使用


您也可以尝试使用AFNetworking,它提供了一个很好的api

,根据文档,您只会有最短的时间来执行操作,例如在应用程序运行到后台时保存任何当前进程的状态,以便在应用程序运行时恢复