为什么 URLSession.shared.data 失败并抱怨未连接的 nw_connection?

kaw*_*wub 7 xcode ios async-await swift urlsession

我对 Swift 编码还很陌生,目前正在开发我的第一个 iOS 应用程序。我的应用程序的一部分必须通过发出请求并解析它返回的 JSON 来与 Spotify API 进行通信。我有一个相对简单的async函数来实现这一点,并且在大多数情况下,它效果很好。不过,我最近以语句的形式向该函数添加了一些附加功能if,用于调整目标 URL 的构建方式。周围的代码是相同的,甚至使用request共享的。URLSession

这是我正在讨论的函数的片段:

// 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)
Run Code Online (Sandbox Code Playgroud)

每当else分支被采用时,代码就可以正常工作并达到我的预期。它不会抛出任何错误,并且不会在控制台中抱怨任何一点。当if分支被采用时,代码在代码片段的最后一行崩溃。通过崩溃,我的意思是一个do-catch块甚至不会捕获错误,所以我认为这是一个非常令人讨厌的异常。在控制台中,它打印以下内容:

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
Run Code Online (Sandbox Code Playgroud)

XCTestCase我正在和一些开车的人一起测试这个。通过案例和失败案例的驱动代码几乎相同,所以我认为它们不是问题。除了 之外self.qe.accessToken(我知道这不适nil用于这两种情况),唯一可能影响最后一行的变量是urlrequest。如果request在这两种情况下都是从 URL 构建的,并且为两者提供了完全相同的标头信息,为什么一个会崩溃而另一个不会呢?

如果我需要进一步解释或者需要提供更多上下文代码,请告诉我。

额外问题:作为 Swift 和 Xcode 的新手,如何调试这种异常?我似乎无法在 swift 中捕获异常,并且似乎无法URLSession.shared.data使用 Xcode 进入函数调用。据我所知,我提供的唯一调试信息是打印到控制台的信息。任何提示将不胜感激!