使用Crashlytics错误日志修复崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Crashlytics错误日志修复崩溃相关的知识,希望对你有一定的参考价值。

我从Crashlytics获得了一个错误日志,内容如下:

Crashed: NSOperationQueue 0x1d003d5a0 (QOS: UNSPECIFIED)
0  (App Name)            0x100710fa4 specialized ViewController.(getHistoricalData(url : String, currency : String) -> ()).(closure #1) (ViewController.swift:1324)
1  (App Name)             0x1007029f8 ViewController.(getHistoricalData(url : String, currency : String) -> ()).(closure #1) (ViewController.swift)
2  (App Name)             0x100712178 partial apply for ViewController.(getHistoricalData(url : String, currency : String) -> ()).(closure #1) (ViewController.swift)
3  (App Name)             0x1006fd448 @callee_owned (@owned Data?, @owned URLResponse?, @owned Error?) -> ()NSData?NSData (ViewController.swift)
4  CFNetwork                      0x181fd1d68 __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 32
5  CFNetwork                      0x181fea6bc __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 152
6  Foundation                     0x18241dba0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
7  Foundation                     0x18235d894 -[NSBlockOperation main] + 72
8  Foundation                     0x18234d4c4 -[__NSOperationInternal _start:] + 848
9  libdispatch.dylib              0x1813c6a14 _dispatch_client_callout + 16
10 libdispatch.dylib              0x1814029c4 _dispatch_block_invoke_direct$VARIANT$armv81 + 280
11 libdispatch.dylib              0x1813c6a14 _dispatch_client_callout + 16
12 libdispatch.dylib              0x1814029c4 _dispatch_block_invoke_direct$VARIANT$armv81 + 280
13 libdispatch.dylib              0x181402878 dispatch_block_perform$VARIANT$armv81 + 104
14 Foundation                     0x18241f878 __NSOQSchedule_f + 376
15 libdispatch.dylib              0x1813c6a14 _dispatch_client_callout + 16
16 libdispatch.dylib              0x181403640 _dispatch_continuation_pop$VARIANT$armv81 + 420
17 libdispatch.dylib              0x181401fe8 _dispatch_async_redirect_invoke$VARIANT$armv81 + 596
18 libdispatch.dylib              0x1814082a4 _dispatch_root_queue_drain + 568
19 libdispatch.dylib              0x181408008 _dispatch_worker_thread3 + 112
20 libsystem_pthread.dylib        0x18166f06c _pthread_wqthread + 1268
21 libsystem_pthread.dylib        0x18166eb6c start_wqthread + 4

其中(App Name)对应我应用程序的名称。据我所知,错误发生在ViewController.swift的第1324行,在函数getHistoricalData(url : String, currency : String)中。功能如下:

func getHistoricalData(url: String, currency: String){
        var Currenturl = NSURL(string: url)


        if Reachability.isConnectedToNetwork() {

            URLSession.shared.dataTask(with: (Currenturl as URL?)!, completionHandler: {(data, response, error) -> Void in
                let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
                if let jsonObj = jsonObj  {


                  // do some stuff
                }
            }).resume()

        }
    }

第1324行对应于let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary

我已经尝试了一些不同的解开jsonObj的方法来试图阻止这次崩溃。最初我有像if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {这样的东西,但同样的问题正在发生。

这与我解开的方式有关吗?任何想法/指针将不胜感激!

答案

如果错误在let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary中,则有两个明显的错误原因:

  • 无论出于什么原因,对NSDictionary的演员都失败了
  • 强行展开data失败。

不要使用!强制解包服务器接收的数据,它可能是零。请改用以下代码:

guard let data = data, let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary else { // Handle error here }

以上是关于使用Crashlytics错误日志修复崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在本机反应中找不到 Firebase crashlytics 崩溃日志?

如何修复此 Crashlytics 缺少依赖项异常?

Crashlytics 使 TestFlight 应用程序崩溃,但未找到任何崩溃日志

是否可以在 android 中使用 Fabric Crashlytics 将崩溃日志保存到电子邮件中?

TestFlight 的所有新版本在发布时突然开始崩溃,并且在 Crashlytics 上没有崩溃日志

将 LogCat 日志添加到 Firebase Crashlytics