I'm pretty new to coding in Swift and am currently working on my very first iOS app. Part of my app has to talk to the Spotify API by making requests and parsing JSON that it returns. I have a relatively simple async
function that I use to achieve this and for the most part, it works great. However, I've recently added some additional functionality to the function in the form of an if
statement that adjusts how the target URL is built. The surrounding request
code is the same and even the shared URLSession
is used.
Here's a snippet from the function I'm talking about:
// build the request
var request: URLRequest
if (id != nil) {
// failing case
let url = URL(string: "https://api.spotify.com/v1/track/\(id!)")!
request = URLRequest(url: url)
} else {
// working case
var url = URLComponents(string: "https://api.spotify.com/v1/search")!
url.queryItems = [
URLQueryItem(name: "q", value: self.queryString),
URLQueryItem(name: "type", value: self.typeString)
]
request = URLRequest(url: url.url!)
}
request.httpMethod = "GET"
request.addValue("Bearer \(self.qe.accessToken!)", forHTTPHeaderField: "Authorization")
// where the code crashes
let (data, response) = try await URLSession.shared.data(for: request)
Whenever the else
branch is taken, the code works fine and does what I expected it to. It throws no errors and does not complain a single bit in the console. When the if
branch is taken, the code crashes on the last line of the snippet. And by crash, I mean that a do-catch
block won't even catch the error, so I imagine it's a pretty nasty exception. In the console, it prints the following:
2021-10-26 10:44:47.114383-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_protocol_metadata_internal [C2] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2021-10-26 10:44:47.114527-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_protocol_metadata_internal [C2] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2021-10-26 10:44:47.114671-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_protocol_metadata_internal [C2] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2021-10-26 10:44:47.114814-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_protocol_metadata_internal [C2] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2021-10-26 10:44:47.114954-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_connected_local_endpoint [C2] Client called nw_connection_copy_connected_local_endpoint on unconnected nw_connection
2021-10-26 10:44:47.115077-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_connected_remote_endpoint [C2] Client called nw_connection_copy_connected_remote_endpoint on unconnected nw_connection
2021-10-26 10:44:47.115164-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_connected_path [C2] Client called nw_connection_copy_connected_path on unconnected nw_connection
2021-10-26 10:44:47.115393-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_protocol_metadata_internal [C2] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2021-10-26 10:44:47.115713-0400 AnyPlay[12545:708947] [connection] nw_connection_copy_metadata [C2] Client called nw_connection_copy_metadata on unconnected nw_connection
I'm testing this with some XCTestCase
's doing the driving. The driving code for the passing case and the failing case are nearly identical so I don't think they're the problem. With the exception of self.qe.accessToken
, which I know is not nil
for both cases, the only variables that could affect the last line are url
and request
. If request
is being built from a URL in both situations, and the exact same header info is provided for both, why would one crash and the other not?
Please let me know if I need to explain anything further or if I need to provide more surrounding code for context.
Bonus question: as a newbie to Swift and Xcode, how would one go about debugging this kind of exception? I can't seem to catch the exception in swift and I can't seem to step into the URLSession.shared.data
function call with Xcode. And as far as I can tell, the only debugging info I'm provided with is what is printed to console. Any tips would be appreciated!