swift - How can I display an error and terminate the app when `NSURLSession` times out? -


so i'm trying run nsurlsession datatask on intranet-- if user isn't connected intranet, request time out.

in case, want display error user, , terminate app

i have this:

    let task = session.datatask(with: request, completionhandler: { (data, response, error) in         if error != nil {             let alert = nsalert()             alert.messagetext = "network error"             alert.informativetext = "make sure connected internal network.  closing..."             alert.alertstyle = nsalertstyle.warning             alert.addbutton(withtitle: "ok")             dispatchqueue.main.async {                 if alert.runmodal() == nsalertfirstbuttonreturn {                     nsapplication.shared().terminate(self)                 }             }         }         let httpresponse = response as! httpurlresponse 

but code crashes @ alert.runmodal()

current stack trace: 0    libswiftcore.dylib                 0x0000000102b99130 swift_reporterror + 129 1    libswiftcore.dylib                 0x0000000102bb5b50 _swift_stdlib_reportfatalerror + 60 2    libswiftcore.dylib                 0x00000001029a6250 specialized specialized staticstring.withutf8buffer<a> ((unsafebufferpointer<uint8>) -> a) -> + 342 3    libswiftcore.dylib                 0x0000000102b20e90 partial apply (_fatalerrormessage(staticstring, staticstring, file : staticstring, line : uint, flags : uint32) -> never).(closure #2) + 109 4    libswiftcore.dylib                 0x00000001029a6250 specialized specialized staticstring.withutf8buffer<a> ((unsafebufferpointer<uint8>) -> a) -> + 342 5    libswiftcore.dylib                 0x0000000102ad39a0 specialized _fatalerrormessage(staticstring, staticstring, file : staticstring, line : uint, flags : uint32) -> never + 96 6    myapp                         0x00000001001bf2e0 appdelegate.(_getserviceticket(completion : () -> ()?) -> ()).(closure #1) + 1063 7    myapp                         0x00000001001c2670 thunk + 203 8    cfnetwork                          0x00007fffb00d8381 __75-[__nsurlsessionlocal taskforclass:request:uploadfile:bodydata:completion:]_block_invoke + 19 9    cfnetwork                          0x00007fffb00d7b0c __49-[__nscflocalsessiontask _task_onqueue_didfinish]_block_invoke + 308 10   foundation                         0x00007fffb29b2f12 __nsblockoperation_is_calling_out_to_a_block__ + 7 11   foundation                         0x00007fffb29b2b97 -[nsblockoperation main] + 101 12   foundation                         0x00007fffb29b1084 -[__nsoperationinternal _start:] + 672 13   foundation                         0x00007fffb29ad112 __nsoqschedule_f + 201 14   libdispatch.dylib                  0x00000001032ee784 _dispatch_client_callout + 8 15   libdispatch.dylib                  0x0000000103305872 _dispatch_queue_serial_drain + 205 16   libdispatch.dylib                  0x00000001032f760c _dispatch_queue_invoke + 1174 17   libdispatch.dylib                  0x00000001032f076d _dispatch_root_queue_drain + 671 18   libdispatch.dylib                  0x00000001032f06ab _dispatch_worker_thread3 + 114 19   libsystem_pthread.dylib            0x00000001033652c7 _pthread_wqthread + 1299 20   libsystem_pthread.dylib            0x00000001033652a8 start_wqthread + 13 

anybody have suggestions?

if @ declaration completion handler datatask(), see 3 parameters, data, response, , error, optional. in case error non-nil, data , response nil, since program not able contact server in order response back. therefore, instead of line:

let httpresponse = response as! httpurlresponse 

you should:

if let httpresponse = response as? httpurlresponse {     // handle response } 

the other option guard response, can present meaningful error user in case not present or in correct format:

guard let httpresponse = response as? httpurlresponse else {     // handle error } 

additionally, since case error non-nil going result in closure of app, rest of processing in method irrelevant in case, may idea stick return @ end of if error != nil block exit after enqueuing alert.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -