在下载如何处理睡眠后iOS应用崩溃的问题吗?

时间:2019-07-12 03:10:59

标签: ios swift urlsession

当我弹出ViewController时,我将文件下载到ComicListViewcontroller中的表格单元中,然后转到ComicListViewcontroller,然后再次将popViewController返回至ComicViewcontroller。如果它休眠,那么我回家时该应用将崩溃

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        PublicClass.isFinishDownloading = false

        if let downloadUrl = downloadTask.originalRequest?.url?.absoluteString,
            let downloader = PublicClass.downloadFileLink[downloadUrl] {

            downloader.progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)

            self.totalItems = "\(PublicClass.downloadInProcess.count) Items"
            self.taskdownload = downloadTask

            DispatchQueue.main.async {
                print(downloader.progress)
                if(String(format: "%.1f%%", downloader.progress * 100) == "100.0%"){
                    print(String(format: "%.1f%%", downloader.progress * 100))
                    self.presenter.getComicList()
                    self.root.tableView.reloadData()
                }
            }
        }
    }
  

2019-07-12 10:02:55.617104 + 0700 Sariputta [2186:66954]   -[Sariputta.ComicListViewController originalRequest]:无法识别的选择器发送到实例0x7fe184599f30 2019-07-12   10:02:55.630284 + 0700 Sariputta [2186:66954]由于以下原因终止了应用   未捕获的异常“ NSInvalidArgumentException”,原因:   '-[Sariputta.ComicListViewController originalRequest]:无法识别   选择器发送到实例0x7fe184599f30'第一个抛出调用堆栈:(     0 CoreFoundation 0x000000011058e1bb    exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010f6b0735 objc_exception_throw + 48 2 CoreFoundation
  0x00000001105acf44-[NSObject(NSObject)didNotRecognizeSelector:] +   132 3 UIKitCore 0x0000000118b27b4a   -[UIResponder didNotRecognizeSelector:] + 287 4 CoreFoundation 0x0000000110592ed6 ___ forwarding _
+ 1446 5 CoreFoundation
  0x0000000110594da8 _CF_forwarding_prep_0 + 120 6 Sariputta
  0x000000010b34cbf9   $ S9Sariputta23ComicListViewControllerC10urlSession_12downloadTask12didWriteData17totalBytesWritten0mn10ExpectedToK0ySo12NSURLSessionC_So0r8DownloadI0Cs5Int64VA2NtF   + 233 7 Sariputta 0x000000010b34d8b9 $ S9Sariputta23ComicListViewControllerC10urlSession_12downloadTask12didWriteData17totalBytesWritten0mn10ExpectedToK0ySo12NSURLSessionC_So0r8DownloadI0Cs5Int64VA2NtFTo   + 105 8 CoreFoundation 0x00000001104cdbac CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER + 12 9 CoreFoundation 0x00000001104cd02f   _CFXRegistrationPost + 447 10 CoreFoundation 0x00000001104ccd71 _CFXNotificationPost_block_invoke + 225 11   核心基金会0x00000001105b3ae2   -[_ CFXNotificationRegistrar查找:对象:观察者:枚举器:] + 1826 12 CoreFoundation 0x00000001104cc694   _CFXNotificationPost + 964 13 Foundation 0x000000010e0b6589-[NSNotificationCenter   postNotificationName:object:userInfo:] + 66 14 UIKitCore
  0x0000000118b129cc-[UIApplication _sendWillEnterForegroundCallbacks]   + 136 15 UIKitCore 0x0000000118317159-[__ UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 2742 16 UIKitCore
  0x0000000118314cb5 __82-[_ UIApplicationCanvas   _transitionLifecycleStateWithTransitionContext:completion:] _ block_invoke   + 795 17 UIKitCore 0x000000011831495f-[_ UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 435 18 UIKitCore 0x0000000118319a90   __125-[_ UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] _ block_invoke   + 584 19 UIKitCore 0x000000011831a80e _performActionsWithDelayForTransitionContext + 100 20 UIKitCore 0x00000001183197ef-[_ UICanvasLifecycleSettingsDiffAction   performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]   + 221 21 UIKitCore 0x000000011831e93a-[_ UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392 22 UIKitCore 0x000000011869b1f5   -[UIApplicationSceneClientAgent场景:handleEvent:withCompletion:] + 502 23 FrontBoardServices 0x000000011b876fdb   __80- [FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:] _ block_invoke.359   + 213 24 libdispatch.dylib 0x00000001119de602 _dispatch_client_callout + 8 25 libdispatch.dylib 0x00000001119e1b78 _dispatch_block_invoke_direct + 301 26   FrontBoardServices 0x000000011b8b5ba8   __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK
+ 30 27 FrontBoardServices 0x000000011b8b5860   -[FBSSerialQueue _performNext] + 457 28 FrontBoardServices 0x000000011b8b5e40-[FBSSerialQueue _performNextFromRunLoopSource] +   45 29 CoreFoundation 0x00000001104f3721    CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 30 CoreFoundation 0x00000001104f2f93   __CFRunLoopDoSources0 + 243 31 CoreFoundation 0x00000001104ed63f __CFRunLoopRun + 1263 32 CoreFoundation
  0x00000001104ece11 CFRunLoopRunSpecific + 625 33 GraphicsServices
  0x000000011621d1dd GSEventRunModal + 62 34 UIKitCore
  0x0000000118af981d UIApplicationMain + 140 35 Sariputta
  0x000000010b47e3e7主+ 71 36 libdyld.dylib
  0x0000000111a54575开始+ 1 37 ???
  0x0000000000000001 0x0 +1)libc ++ abi.dylib:终止于   类型为NSException的未捕获异常

1 个答案:

答案 0 :(得分:0)

我在ComicListViewController viewDidLoad()中知道答案

if PublicClass.isDownloading == true {
            NotificationCenter.default.addObserver(self, selector: #selector(urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)), name: UIApplication.willEnterForegroundNotification, object: nil)
            UIApplication.shared.isIdleTimerDisabled = true
        }

如果我删除该代码,它不会崩溃,但是当我进入该代码时,我需要该代码才能继续下载文件。