Cyan Lee
Cyan Lee

Reputation: 341

Crashes when calling delegate method inside URLSession closure

I created class named FileTransferManager which manages upload/download task using URLSession.

Because of code's length, I created a gist of my code. : https://gist.github.com/Cyanide7523/eb2f7a743459055e13b8568a51f644f3

And I created delegate protocol to recognize the transfer result.

This is a sample usage of this class :

class SampleViewController: UIViewController, FileTransferDelegate{

    let fileMan = FileTransferManager()

    fileMan.delegate = self

    fileMan.download( /* Some parameters */ )

    func fileTransferManager(_ sender: FileTransferManager, didSucceedDownload data: Data, ...) {

        print("Download Succeed!")
    }
}

But when FileTransferManager calls delegate functions, App always crashes with message "unrecognized selector sent to instance" and I can't figure out why does this crashes.

+++ Error logs

2018-06-27 14:31:57.851160+0900 Project[1428:2194695] -[Project.InitialViewController fileTransferManagerWithSender:willDownload:at:]: unrecognized selector sent to instance 0x10207a0e0
2018-06-27 14:31:57.851783+0900 Project[1428:2194695] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project.InitialViewController fileTransferManagerWithSender:willDownload:at:]: unrecognized selector sent to instance 0x10207a0e0'
*** First throw call stack:
(0x184d1ad8c 0x183ed45ec 0x184d28098 0x18ee0adb0 0x184d202d4 0x184c0641c 0x1003974b0 0x100399094 0x100396d8c 0x1852a9e4c 0x1852c2b6c 0x185742e88 0x1856848d0 0x185683cac 0x101ec119c 0x101ecd7cc 0x101ec119c 0x101ecd7cc 0x101ecd6b0 0x185744750 0x101ec119c 0x101ece454 0x101eccd44 0x101ed27c8 0x101ed2500 0x18493ffac 0x18493fb08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Upvotes: 0

Views: 439

Answers (1)

Srj0x0
Srj0x0

Reputation: 456

Do you implement @objc optional func fileTransferManager(_ sender: FileTransferManager, willDownload contentID: String, at room: String) method in InitialViewController? Also, make your FileTransferManager delegate reference "weak" and remove all force-unwrap's when you call delegate methods (just replace "!" by "?").

Upvotes: 1

Related Questions