已崩溃:com.apple.NSURLSession-Delegate-EXC_BAD_ACCESS [英] Crashed: com.apple.NSURLSession-delegate - EXC_BAD_ACCESS

查看:33
本文介绍了已崩溃:com.apple.NSURLSession-Delegate-EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌的Crashlytics上看到了许多用户的崩溃。我在下面发布了堆栈跟踪,但请记住,许多其他URL请求都会发生这种情况。在过去的几个月里,我的URL请求函数都没有被修改过,所以我想知道这是不是苹果的新iOS版本的错误?如果有人能为我分解堆栈跟踪,并解释这是苹果的错误还是我可以修复的东西,那就太好了。

Crashed: com.apple.NSURLSession-delegate
0  r6stats                        0x100b29750 closure #1 in UbiApi.GetServerToken(account:) + 28 (UbiApi.swift:28)
1  r6stats                        0x100b282a8 thunk for @escaping @callee_guaranteed (@guaranteed Data?, @guaranteed NSURLResponse?, @guaranteed Error?) -> () + 4343775912 (<compiler-generated>:4343775912)
2  CFNetwork                      0x1919f63dc CFNetServiceBrowserSearchForServices + 79304
3  CFNetwork                      0x191a08768 _CFHTTPMessageSetResponseProxyURL + 9196
4  libdispatch.dylib              0x190fbda84 _dispatch_call_block_and_release + 32
5  libdispatch.dylib              0x190fbf81c _dispatch_client_callout + 20
6  libdispatch.dylib              0x190fc7004 _dispatch_lane_serial_drain + 620
7  libdispatch.dylib              0x190fc7c34 _dispatch_lane_invoke + 456
8  libdispatch.dylib              0x190fd24bc _dispatch_workloop_worker_thread + 764
9  libsystem_pthread.dylib        0x1dcf367a4 _pthread_wqthread + 276
10 libsystem_pthread.dylib        0x1dcf3d74c start_wqthread + 8
func GetServerToken(account: Int) {
    guard let url = URL(string: ApiHeaders.Tokens().url + String(account)) else { print("Invalid URL"); return }
        
    var urlRequest = URLRequest(url: url)
    urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    urlRequest.httpMethod = "GET"
        
    URLSession.shared.dataTask(with: urlRequest) { (data, res, err) in
        guard let data = data else { return }

        do {
            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
                
            let json = try decoder.decode(LoginResponse.self, from: data)
                
            let expirationDate : Date = json.expiration!
            let token = "Ubi_v1 t=" + json.ticket!
            let sessionId = json.sessionId
                
            UserDefaults.standard.setValue(expirationDate, forKey: "r6_token_expiration_(account)")
            UserDefaults.standard.setValue(token, forKey: "r6_token_(account)")
            UserDefaults.standard.setValue(sessionId, forKey: "r6_token_sessionId_(account)")
                
            UbiApi.accountsNeeded -= 1 // This is declared on line 28
            self.CheckIfTokensAreDone()
        }
        catch {}
    }.resume()
}

推荐答案

代码的最后一部分是线程不安全的:

    UserDefaults.standard.setValue(expirationDate, forKey: "r6_token_expiration_(account)")
    UserDefaults.standard.setValue(token, forKey: "r6_token_(account)")
    UserDefaults.standard.setValue(sessionId, forKey: "r6_token_sessionId_(account)")
            
    UbiApi.accountsNeeded -= 1 // This is declared on line 28
    self.CheckIfTokensAreDone()

将其序列化到安全线程:

DispatchQueue.main.async {
    UserDefaults.standard.setValue(expirationDate, forKey: "r6_token_expiration_(account)")
    UserDefaults.standard.setValue(token, forKey: "r6_token_(account)")
    UserDefaults.standard.setValue(sessionId, forKey: "r6_token_sessionId_(account)")
            
    UbiApi.accountsNeeded -= 1 // This is declared on line 28
    self.CheckIfTokensAreDone()
}

这篇关于已崩溃:com.apple.NSURLSession-Delegate-EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆