gpt4 book ai didi

使用 NSURLSessionDataTask 的 iOS 后台任务

转载 作者:行者123 更新时间:2023-12-01 17:37:03 26 4
gpt4 key购买 nike

我的应用程序中有航类搜索功能,获取数据的时间太长(超过 25 秒)。如果应用程序进入后台或进入休眠模式,互联网连接将断开。

我使用苹果示例编写了以下逻辑,以使 api 请求继续进行,即使应用程序进入后台但它不起作用。

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;
}

以下是委托(delegate)方法
- (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;
}

当应用程序在前台时一切正常,但是一旦我将应用程序放在后台,就会出现错误消息。

Completed with error: Lost connection to background transfer service

最佳答案

您不能将数据任务用于后台传输。这些必须使用下载任务完成:

Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.



这在 Apple 的 documentation 中进行了解释。 .

也一定要看看他们的 background transfer considerations :

With background sessions, because the actual transfer is performed by a separate process and because restarting your app’s process is relatively expensive, a few features are unavailable, resulting in the following limitations...



这里的关键是它在一个单独的进程中运行,无法访问您保存在内存中的数据。它必须通过文件进行路由。

我在一个(长) blog post 中收集了很多关于 iOS 后台传输的信息.

关于使用 NSURLSessionDataTask 的 iOS 后台任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39639268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com