【问题标题】:NSURLSession hangs on iOS 7.0.xNSURLSession 在 iOS 7.0.x 上挂起
【发布时间】:2014-03-21 12:57:26
【问题描述】:

我们已经配置了 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 应用程序:performFetchWithCompletionHandler:]

  • 但是 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 问题。有什么好的解决办法吗?

【问题讨论】:

  • 尝试在类中保留委托队列或使用主队列
  • @phix23 是的,我们使用主队列。我们在 iOS 7.1 上没有任何问题。这意味着我们遵循共同的工作流程。
  • 您创建一个新的 NSOperationQueue 并将其用作代码中的 delegateQueue。尝试将 nil 作为 delegateQueue 传递,然后 NSURLSession 将创建它自己的队列。
  • @phix23 谢谢!我们尝试过,但结果相同)

标签: ios iphone ios7 nsurlsession nsurlsessiondownloadtask


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多