热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

NSURLSessionDelegatefailingforlargefileson4G&iOS10only

ImhavingstrangeproblemwithNSURLSessionDelegate.Heresmycodefirst:我对NSURLSessionDelegate有一

I'm having strange problem with NSURLSessionDelegate. Here's my code first:

我对NSURLSessionDelegate有一些奇怪的问题。这是我的代码:

    class NetworkHandler: NSObject,NSURLSessionDelegate,NSURLSessionDownloadDelegate {

    lazy var downloadsSession: NSURLSession = {
        let cOnfiguration= NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("abc")
        let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
        return session
    }()

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

        print("finished")
    }

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

       print("\(String(format: "%.1f%% of %@",  progress * 100, totalSize))" )
}

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
        print("didResumeAtOffset: \(fileOffset)")
    }

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        print("didCompleteWithError error=\(error)");
    }

    func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            if let completiOnHandler= appDelegate.backgroundSessionCompletionHandler {
                appDelegate.backgroundSessiOnCompletionHandler= nil
                dispatch_async(dispatch_get_main_queue(), {
                    completionHandler()
                })
            }
        }
    }
}

Here's my output when my file is ~20MB and I'm on 4G/LTE:

这是我的输出,当我的文件大约是20MB并且我在4G / LTE时:

didCompleteWithError error=Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL})

I've already checked my url has http and its a valid url. This code works 100% of the time when I'm on wifi or when file size is <~10MB or NOT iOS10. The file downloads till about 60% and then fail everytime. I'm just out of ideas to even justify myself what could go wrong.

我已经检查过我的网址有http和它的有效网址。当我在wifi上或文件大小<~10MB或不是iOS10时,此代码可以100%正常工作。文件下载到大约60%,然后每次都失败。我只是出于想法,甚至为自己辩解可能出错的地方。

The problem happens when it's iOS10 File size ~20MB 4G/LTE ONLY If I change any of the above 3 criteria, it works.

问题发生在它的iOS10文件大小~20MB 4G / LTE ONLY如果我改变上述3个标准中的任何一个,它就有效。

I tried Apple code and the problem is same

我试过Apple代码,问题是一样的

1 个解决方案

#1


1  

This error typically indicates that the URL scheme is unsupported (i.e. it is something other than http:// https:// or ftp://) or that they depend on an NSURLProtocol subclass that is part of your app (which is not allowed in a background session).

此错误通常表示URL方案不受支持(即,它不是http:// https://或ftp://)或者它们依赖于作为应用程序一部分的NSURLProtocol子类(不允许)在后台会议中)。

If you're getting that error with a supported URL scheme, unless you're doing something weird like subclassing NSURL or NSURLRequest, the problem is probably not something you can work around. File a bug with Apple and provide a minimal code snippet that reproduces the bug.

如果您使用受支持的URL方案获得该错误,除非您正在做一些奇怪的事情,如子类化NSURL或NSURLRequest,问题可能不是您可以解决的问题。向Apple提交一个错误,并提供一个可以重现错误的最小代码段。

And if you are subclassing NSURL or NSURLRequest, just know that these subclasses will not work correctly with NSURLSession—particularly with background sessions.

如果你是NSURL或NSURLRequest的子类,只要知道这些子类在NSURLSession中无法正常工作 - 特别是对于后台会话。

Edit: Apparently in iOS 10, there are known bugs that cause this error code instead of the expected one whenever the network drops out. The fix is to use the provided resume data to continue the transfer whenever it happens.

编辑:显然在iOS 10中,每当网络退出时,都会出现导致此错误代码而非预期错误的已知错误。修复方法是使用提供的恢复数据在发生时继续传输。


推荐阅读
author-avatar
mobiledu2502926247
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有