zixun / GodEye

Automaticly display Log,Crash,Network,ANR,Leak,CPU,RAM,FPS,NetFlow,Folder and etc with one line of code based on Swift. Just like God opened his eyes
MIT License
3.85k stars 351 forks source link

启动时崩溃 #10

Closed gyzerocc closed 7 years ago

gyzerocc commented 7 years ago

发生崩溃的类:NetworkRecordModel.swift

发生崩溃的方法:fileprivate func initialize(response: HTTPURLResponse?, data:Data?)

具体位置:

do {
            let returnValue = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            let data = try JSONSerialization.data(withJSONObject: returnValue)
            return String(data: data, encoding: .utf8)
        } catch  {
            return nil
        }

异常详情:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bb8fd4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010b5f121e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010bbf92b5 +[NSException raise:format:] + 197
    3   Foundation                          0x000000010b0e3500 +[NSJSONSerialization dataWithJSONObject:options:error:] + 249
    4   GodEye                              0x00000001072fc03e _TFC6GodEye18NetworkRecordModelP33_CF214E9D732CCEC1FB66110322E8598F4jsonfT4fromGSqV10Foundation4Data__GSqSS_ + 750
    5   GodEye                              0x00000001072f73a8 _TFC6GodEye18NetworkRecordModelP33_CF214E9D732CCEC1FB66110322E8598F10initializefT8responseGSqCSo15HTTPURLResponse_4dataGSqV10Foundation4Data__T_ + 2536
    6   GodEye                              0x00000001072f5f48 _TFC6GodEye18NetworkRecordModelcfT7requestGSqV10Foundation10URLRequest_8responseGSqCSo15HTTPURLResponse_4dataGSqVS1_4Data__S0_ + 1288
    7   GodEye                              0x00000001072f6076 _TFC6GodEye18NetworkRecordModelCfT7requestGSqV10Foundation10URLRequest_8responseGSqCSo15HTTPURLResponse_4dataGSqVS1_4Data__S0_ + 70
    8   GodEye                              0x00000001072b23f1 _TFC6GodEye17ConsoleController18networkEyeDidCatchfT4withGSqV10Foundation10URLRequest_8responseGSqCSo11URLResponse_4dataGSqVS1_4Data__T_ + 497
    9   GodEye                              0x00000001072b277e _TToFC6GodEye17ConsoleController18networkEyeDidCatchfT4withGSqV10Foundation10URLRequest_8responseGSqCSo11URLResponse_4dataGSqVS1_4Data__T_ + 206
    10  GodEye                              0x00000001072b28a9 _TTDFC6GodEye17ConsoleController18networkEyeDidCatchfT4withGSqV10Foundation10URLRequest_8responseGSqCSo11URLResponse_4dataGSqVS1_4Data__T_ + 201
    11  GodEye                              0x00000001072b27d1 _TTWC6GodEye17ConsoleController16NetworkEye_swift18NetworkEyeDelegateS_FS2_18networkEyeDidCatchfT4withGSqV10Foundation10URLRequest_8responseGSqCSo11URLResponse_4dataGSqVS3_4Data__T_ + 65
    12  NetworkEye_swift                    0x00000001083b8b03 _TFC16NetworkEye_swift11EyeProtocol11stopLoadingfT_T_ + 627
    13  NetworkEye_swift                    0x00000001083b8c52 _TToFC16NetworkEye_swift11EyeProtocol11stopLoadingfT_T_ + 34
    14  CFNetwork                           0x0000000105fc95ca _ZN16CFURLProtocol_NS21forgetProtocolClient0Ev + 100
    15  CFNetwork                           0x0000000105fc9553 ___ZN16CFURLProtocol_NS20forgetProtocolClientEv_block_invoke + 18
    16  libdispatch.dylib                   0x000000010d5050cd _dispatch_client_callout + 8
    17  libdispatch.dylib                   0x000000010d4df9a0 _dispatch_block_invoke_direct + 567
    18  CFNetwork                           0x0000000105fc74c4 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 24
    19  CoreFoundation                      0x000000010bae03f4 CFArrayApplyFunction + 68
    20  CFNetwork                           0x0000000105fc73bd _ZN19RunloopBlockContext7performEv + 137
    21  CFNetwork                           0x0000000105fc7256 _ZN17MultiplexerSource7performEv + 282
    22  CFNetwork                           0x0000000105fc7078 _ZN17MultiplexerSource8_performEPv + 72
    23  CoreFoundation                      0x000000010bb34761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    24  CoreFoundation                      0x000000010bb1998c __CFRunLoopDoSources0 + 556
    25  CoreFoundation                      0x000000010bb18e76 __CFRunLoopRun + 918
    26  CoreFoundation                      0x000000010bb18884 CFRunLoopRunSpecific + 420
    27  CFNetwork                           0x00000001061e03d6 _ZL27_privateRunloopEmulationSetPv + 258
    28  libsystem_pthread.dylib             0x000000010d8acaab _pthread_body + 180
    29  libsystem_pthread.dylib             0x000000010d8ac9f7 _pthread_body + 0
    30  libsystem_pthread.dylib             0x000000010d8ac1fd thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
zixun commented 7 years ago

貌似是因为你的json数据有问题,试试在

let data = try JSONSerialization.data(withJSONObject: returnValue)

前面加一行:

guard JSONSerialization.isValidJSONObject(returnValue) else {
       return nil;
 }

看看有啥效果

等待你的回复

gyzerocc commented 7 years ago

嗯,加上这句代码后不会发生崩溃了!

我能问个问题吗?swift怎样输出ASL?我使用print()发现不行。。

zixun commented 7 years ago

print不会走asl的,只有NSLog()才会走ASL,并且在Swift工程下,NSLog()只有在模拟器上才会走ASL,OC的话真机也会,算是Swift对性能的优化。因此我封装了一个Log4G的库来做日志记录,可以参考CocoaChinaPlus,关于日志监控的所有实现和坑我都写在了《iOS监控编程》里,其中日志相关的是可以试读的

gyzerocc commented 7 years ago

原来如此,谢谢哈