gpt4 book ai didi

swift - Alamofire 导致 iPhone 5 崩溃

转载 作者:行者123 更新时间:2023-11-28 08:19:38 24 4
gpt4 key购买 nike

我目前允许一些用户通过 TestFlight 访问我的应用程序,以便他们可以对其进行测试。在每部 iPhone 上,该应用程序都能完美运行,没有任何问题,但在 iPhone 5 上,该应用程序每次都会崩溃。在 xCode 模拟器中,一切正常,但在真实设备上却不行。

看起来是 Alamofire 导致了崩溃。问题出在 Alamofire>Source>Features>ResponseSerialization.swift>response(queue:completionHandler:)

这是函数的代码:

/// Adds a handler to be called once the request has finished.
///
/// - parameter queue: The queue on which the completion handler is dispatched.
/// - parameter completionHandler: The code to be executed once the request has finished.
///
/// - returns: The request.
@discardableResult
public func response(queue: DispatchQueue? = nil, completionHandler: @escaping (DefaultDataResponse) -> Void) -> Self {
delegate.queue.addOperation {
(queue ?? DispatchQueue.main).async {
var dataResponse = DefaultDataResponse(
request: self.request,
response: self.response,
data: self.delegate.data,
error: self.delegate.error
)

dataResponse.add(self.delegate.metrics)

completionHandler(dataResponse)
}
}

return self
}

completionHandler(dataResponse) 看起来就是问题所在。

下面还有xCode的截图

enter image description here

这是与 Alamofire 相关的问题吗?因为在所有其他设备(5s、6 Plus、7 Plus、SE 和 7)上它都可以正常工作。崩溃发生在 iPhone 连接到 WiFi 和使用移动网络时。

感谢任何提示!

编辑:这应该是启动应用程序时调用的代码部分:

Alamofire.request("https://app.site.tld/mobile/ios", parameters: parameters).response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Error: \(response.data)")

if response.response != nil{
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")

let weatherDataArr = utf8Text.components(separatedBy: "~")
guard let hash = String(weatherDataArr[0]) else {
completion(nil)
return
}
do {
//working with received data.
//let currData: wData = try wData(hash: hash, ....)

completion(currData)
} catch {
print("error creating Object: \(error)")
completion(nil)
}

}
else {
completion(nil)
}
}else {
completion(nil)
}
}

刚刚在 xCode 中发现一条警告:

'catch' block is unreachable because no errors are thrown in 'do' block

最佳答案

首先简化以消除困惑

Alamofire.request("https://app.site.tld/mobile/ios", parameters: parameters).response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Error: \(response.data)")

var result: wData? = nil
defer { completion(result) }

guard response.response != nil else { return }

if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")

let weatherDataArr = utf8Text.components(separatedBy: "~")

guard let hash = String(weatherDataArr[0]) else { return }

//working with received data.
//let currData: wData = try wData(hash: hash, ....)

result = currData
}
}

然后我们可以问问自己

  1. 您如何 100% 确定 weatherDataArr 中有元素?如果没有,使用 guard let hash = weatherDataArr.first else { return }
  2. //working with received data. 会发生什么?

关于swift - Alamofire 导致 iPhone 5 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41663782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com