0
votes

I download file in a table cell in ComicListViewcontroller, when I popViewController, and then get to ComicListViewcontroller, and then popViewController back again to ComicViewcontroller. If it sleeps, the app will crash when I go to home

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]: unrecognized selector sent to instance 0x7fe184599f30 2019-07-12 10:02:55.630284+0700 Sariputta[2186:66954] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Sariputta.ComicListViewController originalRequest]: unrecognized selector sent to instance 0x7fe184599f30' First throw call stack: ( 0 CoreFoundation 0x000000011058e1bb exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010f6b0735 objc_exception_throw + 48 2 CoreFoundation
0x00000001105acf44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 UIKitCore 0x0000000118b27b4a -[UIResponder doesNotRecognizeSelector:] + 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 CoreFoundation 0x00000001105b3ae2 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 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 scene: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 main + 71 36 libdyld.dylib
0x0000000111a54575 start + 1 37 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

1
Somewhere you are attempting to access originalRequest on an instance of ComicListViewController - This view controller doesn't have that property or function.Paulw11
Why it access it? Meanwhile, it is still in ComicViewControllernature realm
I don't know - It is your code - You need to look at the line that the stack trace indicates has the problemPaulw11
It does access it and when I po it says:rror: Execution was interrupted, reason: internal ObjC exception breakpoint(-5).. The process has been returned to the state before expression evaluation.nature realm

1 Answers

0
votes

I have known the answer, in 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
        }

If I delete that code it doesn't crash, but I need that code to continue download file when get into it.