Skip to content

[SR-5548] Crash due to invalid casting in URLSession #3825

Closed
@pushkarnk

Description

@pushkarnk
Previous ID SR-5548
Radar None
Original Reporter @pushkarnk
Type Bug
Status Closed
Resolution Done
Environment

Linux

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @pushkarnk
Priority Medium

md5: bb295e9a2444101a117aa7e3c0ec7c0d

Issue Description:

This happens with a delegate class that implements URLSessionDownloadDelegate is used with a DataTask.

import Foundation
import Dispatch

class SharedDelegate: NSObject {
}

extension SharedDelegate: URLSessionDataDelegate {
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        print("Data task completed")
    }
}

extension SharedDelegate: URLSessionDownloadDelegate {
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    }
}

let sharedDelegate = SharedDelegate()
let session = URLSession(configuration: .default, delegate: sharedDelegate, delegateQueue: nil)
let dataRequest = URLRequest(url: URL(string: "http://httpbin.org/get")!)
let dataTask = session.dataTask(with: dataRequest)

dataTask.resume()

dispatchMain()

Expected output:
Data task completed

Actual output:
Could not cast value of type 'Foundation.URLSessionDataTask' (0x7f1d8e643c18) to 'Foundation.URLSessionDownloadTask' (0x7f1d8e6442f8)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions