2

我们已经配置了 NSULRSession

- (NSURLSession *)downloadSession
{
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSOperationQueue *delegateQueue = [[NSOperationQueue alloc] init];

        NSString *identifier = ORDERS_DOWNLOADER_SESSION_IDENTIFIER;
        NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
        session = [NSURLSession sessionWithConfiguration:sessionConfig
                                                                delegate:self
                                                           delegateQueue:delegateQueue];
    });

    return session;
}

在 iOS 7.0.x 上没有构建 SDK 7.0 和 7.1。iOS 7.1 上没有出现问题。

我们经常可以看到以下内容:

  • 开始执行后台下载

    [AppDelegate 应用程序:执行FetchWithCompletionHandler:]

  • 但是 30 秒后,我们的委托没有任何回调。

我们已经实施

 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

 - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
        newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest *))completionHandler

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
 needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
   didSendBodyData:(int64_t)bytesSent
    totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
  • 我们没有任何回调。

可能有什么问题?它看起来像 iOS 7.0.x 问题。有什么好的解决方法吗?

4

1 回答 1

0

那是iOS的问题

更多信息在这里: https ://devforums.apple.com/message/926113#926113

任何解决方案/解决方法?我看到了同样的问题。

使用基本的 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"..."]] 和 -downloadTaskWithRequest:

几点观察:

  1. 应用程序第一次调用 -downloadTaskWithRequest: 它运行良好。

  2. 对 -downloadTaskWithRequest: 的后续调用返回 nil,直到应用程序被终止。

现在我们只有一种解决方法:多次调用任务创建方法。它对我们有用!通常它对第二次第三次有影响。

_task = [[self downloadSession] downloadTaskWithRequest:request completionHandler:nil];

// Workaround to solve issue https://devforums.apple.com/message/926113
// Occurs consistently on iOS 7.0.5 and lower
// Sometimes _task may not be initialized, so we try again:
if (!_task)
{
    for (int i = 0; i < 5; i++)
    {
        NSLog(@"%s, attempt #%d to recreate download task", __func__, i + 1);

        _task = [[self downloadSession] downloadTaskWithRequest:request completionHandler:nil];
        if (_task)
        {
            break;
        }
    }
}
于 2014-03-24T12:30:47.887 回答