Home > Software engineering >  Why calling a dialog with a struct drops this exception?
Why calling a dialog with a struct drops this exception?

Time:04-14

I'm trying to redirect the output of this API Call decoded in a struct but when I try to use the data to create a dialog it gives me this weird exception. As you can see, the API returns data but only when I create the dialog I see this exception. Can you help me?

Code:

struct rspServerInfo: Codable{
        let ok: Bool
        let info: String
    }
    @IBAction func backendDetails(_ sender: Any) {
        
        guard let url = URL(string: "http://\(hostname):\(port)/STOInfo/ServerInfo")else{
            return
        }

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        let body: [String: AnyHashable] = [
            "username": username,
            "password": password,
        
        ]
        
        request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        let task = URLSession.shared.dataTask(with: request) {data, _, error in
            
            guard let data=data, error == nil else{
                return
            }
            do{
                let response = try JSONDecoder().decode(rspServerInfo.self, from: data)
                print("SUCCESS: \(response)")
                let dialogMessage = UIAlertController(title: "Backend details", message: response.info, preferredStyle: .alert)
                    let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
                         print("Ok button tapped")
                    })
                dialogMessage.addAction(ok)
                self.present(dialogMessage, animated: true, completion: nil)
            }
            catch{
                print(error)
                let dialogMessage = UIAlertController(title: "Backend details", message: "Error retreiving.", preferredStyle: .alert)
                    let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
                         print("Ok button tapped")
                    })
                dialogMessage.addAction(ok)
                self.present(dialogMessage, animated: true, completion: nil)
            }
        }
        task.resume()
    }

Exception: SUCCESS: rspServerInfo(ok: true, info: "STO Backend alpha 1.0") 2022-04-13 18:13:05.599263 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.600031 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.604952 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.605543 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.605700 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.605882 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.606270 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.606668 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.606785 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.606909 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.607200 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.607559 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.608617 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.609164 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.609516 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.609899 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.610557 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.610983 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.611178 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.624518 0200 ServiceTOOLS Control[1843:69401] [Animation] [UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior. trace=( 0 UIKitCore 0x00000001843d8514 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 18552084 1 libdispatch.dylib 0x000000010077dfc8 _dispatch_client_callout 16 2 libdispatch.dylib 0x000000010077f934 dispatch_once_callout 80 3 UIKitCore 0x0000000183389c74 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1453172 4 UIKitCore 0x00000001837ce078 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5927032 5 UIKitCore 0x00000001834f5424 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2941988 6 UIKitCore 0x00000001837e7f34 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 6033204 7 UIKitCore 0x000000018382dba4 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 6319012 8 UIKitCore 0x0000000183882b20 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 6667040 9 UIKitCore 0x00000001837390cc E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5316812 10 UIKitCore 0x00000001832516d4 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 173780 11 UIKitCore 0x000000018335a524 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1258788 12 UIKitCore 0x000000018376fd74 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5541236 13 UIKitCore 0x000000018379541c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5694492 14 UIKitCore 0x0000000183617a20 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 4131360 15 UIKitCore 0x000000018376b980 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5523840 16 UIKitCore 0x00000001833c3a44 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1690180 17 UIKitCore 0x00000001833987c8 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1513416 18 UIKitCore 0x000000018355b0b0 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3358896 19 UIKitCore 0x000000018360f4bc E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 4097212 20 UIKitCore 0x00000001836c9c0c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 4860940 21 UIKitCore 0x00000001834cb638 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2770488 22 UIKitCore 0x000000018346c5c8 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2381256 23 UIKitCore 0x00000001835d289c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3848348 24 UIKitCore 0x00000001834eeea4 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2916004 25 ServiceTOOLS Control 0x0000000100435320 $s20ServiceTOOLS_Control14ViewControllerC14backendDetailsyyypFy10Foundation4DataVSg_So13NSURLResponseCSgs5Error_pSgtcfU 1072 26 ServiceTOOLS Control 0x0000000100435854 $s10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIegggg_So6NSDataCSgAGSo7NSErrorCSgIeyByyy_TR 268 27 CFNetwork 0x00000001816ebb9c CFURLRequestSetMainDocumentURL 2496 28 CFNetwork 0x00000001816fc210 _CFNetworkErrorCopyLocalizedDescriptionWithHostname 11296 29 libdispatch.dylib 0x000000010077c700 _dispatch_call_block_and_release 24 30 libdispatch.dylib 0x000000010077dfc8 _dispatch_client_callout 16 31 libdispatch.dylib 0x0000000100785150 _dispatch_lane_serial_drain 684 32 libdispatch.dylib 0x0000000100785e10 _dispatch_lane_invoke 484 33 libdispatch.dylib 0x000000010079163c _dispatch_workloop_worker_thread 852 34 libsystem_pthread.dylib 0x00000001dc87de10 _pthread_wqthread 284 35 libsystem_pthread.dylib 0x00000001dc87d93c start_wqthread 8 ) 2022-04-13 18:13:05.636837 0200 ServiceTOOLS Control[1843:69401] [Assert] Unsupported use of UIKit view-customization API off the main thread. -setHasDimmingView: sent to <_UIAlertControllerView: 0x12bd46690; frame = (0 0; 320 568); layer = <CALayer: 0x2800dbc80>> 2022-04-13 18:13:05.637270 0200 ServiceTOOLS Control[1843:69401] [Assert] Unsupported use of UIKit view-customization API off the main thread. -setShouldHaveBackdropView: sent to <_UIAlertControllerView: 0x12bd46690; frame = (0 0; 320 568); layer = <CALayer: 0x2800dbc80>> 2022-04-13 18:13:05.637433 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.637616 0200 ServiceTOOLS Control[1843:69401] [Assert] Unsupported use of UIKit view-customization API off the main thread. -setAlignsToKeyboard: sent to <UIAlertControllerView: 0x12bd46690; frame = (0 0; 320 568); layer = <CALayer: 0x2800dbc80>> 2022-04-13 18:13:05.640311 0200 ServiceTOOLS Control[1843:69401] [Assert] Cannot be called with asCopy = NO on non-main thread. 2022-04-13 18:13:05.645960 0200 ServiceTOOLS Control[1843:69401] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes. Stack:( 0 CoreAutoLayout 0x0000000198f94f20 E21B6C11-F8B8-3259-8A95-9836F586EA21 20256 1 CoreAutoLayout 0x0000000198f97b7c E21B6C11-F8B8-3259-8A95-9836F586EA21 31612 2 UIKitCore 0x000000018359caf4 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3627764 3 UIKitCore 0x000000018353b724 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3229476 4 UIKitCore 0x000000018370f810 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5146640 5 UIKitCore 0x000000018331a394 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 996244 6 UIKitCore 0x000000018331a084 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 995460 7 UIKitCore 0x0000000183318a00 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 989696 8 UIKitCore 0x00000001836f7dac E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5049772 9 UIKitCore 0x00000001838530d0 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 6471888 10 UIKitCore 0x000000018376ba8c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 5524108 11 UIKitCore 0x00000001833c3a44 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1690180 12 UIKitCore 0x00000001833987c8 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 1513416 13 UIKitCore 0x000000018355b0b0 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3358896 14 UIKitCore 0x000000018360f4bc E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 4097212 15 UIKitCore 0x00000001836c9c0c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 4860940 16 UIKitCore 0x00000001834cb638 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2770488 17 UIKitCore 0x000000018346c5c8 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2381256 18 UIKitCore 0x00000001835d289c E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 3848348 19 UIKitCore 0x00000001834eeea4 E9BCC2B0-AD59-3D23-87B4-495ABEDF5730 2916004 20 ServiceTOOLS Control 0x0000000100435320 $s20ServiceTOOLS_Control14ViewControllerC14backendDetailsyyypFy10Foundation4DataVSg_So13NSURLResponseCSgs5Error_pSgtcfU 1072 21 ServiceTOOLS Control 0x0000000100435854 $s10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIegggg_So6NSDataCSgAGSo7NSErrorCSgIeyByyy_TR 268 22 CFNetwork 0x00000001816ebb9c CFURLRequestSetMainDocumentURL 2496 23 CFNetwork 0x00000001816fc210 _CFNetworkErrorCopyLocalizedDescriptionWithHostname 11296 24 libdispatch.dylib 0x000000010077c700 _dispatch_call_block_and_release 24 25 libdispatch.dylib 0x000000010077dfc8 _dispatch_client_callout 16 26 libdispatch.dylib 0x0000000100785150 _dispatch_lane_serial_drain 684 27 libdispatch.dylib 0x0000000100785e10 _dispatch_lane_invoke 484 28 libdispatch.dylib 0x000000010079163c _dispatch_workloop_worker_thread 852 29 libsystem_pthread.dylib 0x00000001dc87de10 _pthread_wqthread 284 30 libsystem_pthread.dylib 0x00000001dc87d93c start_wqthread 8 ) 2022-04-13 18:13:05.647262 0200 ServiceTOOLS Control[1843:69401] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' *** First throw call stack: (0x180f83e0c 0x198cd2ee4 0x198f95000 0x198f97b7c 0x18359caf4 0x18353b724 0x18370f810 0x18331a394 0x18331a084 0x183318a00 0x1836f7dac 0x1838530d0 0x18376ba8c 0x1833c3a44 0x1833987c8 0x18355b0b0 0x18360f4bc 0x1836c9c0c 0x1834cb638 0x18346c5c8 0x1835d289c 0x1834eeea4 0x100435320 0x100435854 0x1816ebb9c 0x1816fc210 0x10077c700 0x10077dfc8 0x100785150 0x100785e10 0x10079163c 0x1dc87de10 0x1dc87d93c) libc abi: terminating with uncaught exception of type NSException dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' terminating with uncaught exception of type NSException

CodePudding user response:

The error message give a big clue:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' terminating with uncaught exception of type NSException

The URLRequest run asynchronously on a background thread, including its completion handlers. You (generally) can't do UI work outside of the main thread. To display the results you need to push the operation back onto the main thread:

DispatchQueue.main.async {
   // do UI work
}
  • Related