问题描述
我的应用程序中有飞行搜索功能,这需要很长时间才能获取数据(超过25秒)。如果应用程序进入后台或进入睡眠模式,互联网连接将断开连接。
I have flight search feature in my application which is taking too long to get the data (more than 25 seconds). if application goes background or went to sleep mode, internet connection get disconnected.
我已经使用苹果示例编写了以下逻辑,以使api请求继续运行,即使应用程序转到背景,但它不起作用。
I have written below logic using apple example to make api request keep going even though if app goes to background but it's not working.
self.session = [self backgroundSession];
self.mutableData = [NSMutableData data];
NSURL *downloadURL = [NSURL URLWithString:@"http://jsonplaceholder.typicode.com/photos"];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
self.dataTask = [self.session dataTaskWithRequest:request];
[self.dataTask resume];
- (NSURLSession *)backgroundSession
{
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
以下是委托方法
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
NSLog(@"response: %@", response.debugDescription);
NSURLSessionResponseDisposition disposition = NSURLSessionResponseAllow;
if (completionHandler) {
completionHandler(disposition);
}
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data {
[self.mutableData appendData:data];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
BLog();
if (error == nil)
{
NSData *data = nil;
if (self.mutableData) {
data = [self.mutableData copy];
self.mutableData = nil;
}
NSError* error;
NSArray* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (!json) {
NSLog(@"Error parsing JSON: %@", error);
} else {
NSLog(@"Data: %@", json);
}
}
else
{
NSLog(@"Task: %@ completed with error: %@", task, [error localizedDescription]);
}
double progress = (double)task.countOfBytesReceived / (double)task.countOfBytesExpectedToReceive;
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = progress;
});
self.dataTask =nil;
}
当应用程序处于前台时,一切正常,但只要我将应用程序放在背景低于错误消息。
Everything works fine when application is in foreground but as soon as I put application on background getting below error message.
推荐答案
您不能将数据任务用于后台传输。这些必须使用下载任务完成:
You cannot use data tasks for background transfers. Those must be done using download tasks:
这在Apple的。
另请务必查看他们的:
这里的关键是它在一个单独的进程中运行,无法访问你保存在内存中的数据。它必须通过文件进行路由。
The key here is that it's running in a separate process which cannot access the data you keep in memory. It must be routed through a file.
我在(长)。
这篇关于iOS后台任务使用NSURLSessionDataTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!