热门标签 | 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中,每当网络退出时,都会出现导致此错误代码而非预期错误的已知错误。修复方法是使用提供的恢复数据在发生时继续传输。


推荐阅读
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 如何高效创建和使用字体图标
    在Web和移动开发中,为什么选择字体图标?主要原因是其卓越的性能,可以显著减少HTTP请求并优化页面加载速度。本文详细介绍了从设计到应用的字体图标制作流程,并提供了专业建议。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 深入理解Java泛型:JDK 5的新特性
    本文详细介绍了Java泛型的概念及其在JDK 5中的应用,通过具体代码示例解释了泛型的引入、作用和优势。同时,探讨了泛型类、泛型方法和泛型接口的实现,并深入讲解了通配符的使用。 ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
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社区 版权所有