API 호출시 UI 멈춤 해결

2016. 7. 28. 13:14

확실한 것은 아니지만

post 로 통신을 하면서

전송 완료 에 화면에 관련된 먼가를 하면 친숙한 

에러가 계속 보였다.

계속되는 오류 속에 이거 인듯하다 라는 것을 적어둬야지.

   let postString = "id=\(id)&pw=\(pw)"

                print(postString)

                request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

                

                let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in

                    guard error == nil && data != nil else {                                                          

// check for fundamental networking error

                        print("error=\(error)")

                        return

                    }

                    

                    if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           

// check for http errors

                        print("statusCode should be 200, but is \(httpStatus.statusCode)")

                        print("response = \(response)")

                    }

                    

                    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)


//로그인 성공

                    guard responseString=="false" else {

        self.LoginSuccess()

                        return

                    }

                    //로그인 실패

NSLog("로그인 실패")

                }

                task.resume()



기본소스는 이거 하지만...


self.LoginSuccess() 여기서 화면 전환이 이루어지는데


계속 오류남....




오류내용.


로그인 실패 = Optional(false)

2016-07-28 13:06:45.916 MyAPP[10681:3224463] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/Keyboard/UIKeyboardTaskQueue.m:386

2016-07-28 13:06:45.920 MyAPP[10681:3224463] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',

 reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

*** First throw call stack:

(

0   CoreFoundation                      0x0000000104e61d85 __exceptionPreprocess + 165

1   libobjc.A.dylib                     0x0000000106c05deb objc_exception_throw + 48

2   CoreFoundation                      0x0000000104e61bea +[NSException raise:format:arguments:] + 106

3   Foundation                          0x00000001052b2d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198

4   UIKit                               0x00000001060c4a73 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 165

5   UIKit                               0x000000010592bdc9 -[UIKeyboardImpl setDelegate:force:] + 1085

6   UIKit                               0x0000000105cf7d05 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1040

7   UIKit                               0x0000000105d00824 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 475

8   UIKit                               0x0000000105835c5a -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1175

9   UIKit                               0x000000010583794d -[UIViewController _presentViewController:withAnimationController:completion:] + 4916

10  UIKit                               0x000000010583a85c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489

11  UIKit                               0x000000010583a36b -[UIViewController presentViewController:animated:completion:] + 179

12  MyAPP                          0x0000000104c7050c _TFC10MyAPP14ViewController5alertfT7messageSS_T_ + 892

13  MyAPP                          0x0000000104c71cea _TFFC10MyAPP14ViewController11LoginActionFPs9AnyObject_T_U_FTGSqCSo6NSData_GSqCSo13NSURLResponse_GSqCSo7NSError__T_ + 2250

14  MyAPP                          0x0000000104c72197 _TTRXFo_oGSqCSo6NSData_oGSqCSo13NSURLResponse_oGSqCSo7NSError__dT__XFdCb_dGSqS__dGSqS0__dGSqS1___dT__ + 103

15  CFNetwork                           0x000000010811fb49 __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19

16  CFNetwork                           0x00000001081320f2 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 302

17  Foundation                          0x00000001052db630 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7

18  Foundation                          0x0000000105216805 -[NSBlockOperation main] + 101

19  Foundation                          0x00000001051f9725 -[__NSOperationInternal _start:] + 646

20  Foundation                          0x00000001051f9336 __NSOQSchedule_f + 194

21  libdispatch.dylib                   0x00000001076953eb _dispatch_client_callout + 8

22  libdispatch.dylib                   0x000000010767b82c _dispatch_queue_drain + 2215

23  libdispatch.dylib                   0x000000010767ad4d _dispatch_queue_invoke + 601

24  libdispatch.dylib                   0x000000010767d996 _dispatch_root_queue_drain + 1420

25  libdispatch.dylib                   0x000000010767d405 _dispatch_worker_thread3 + 111

26  libsystem_pthread.dylib             0x00000001079d24de _pthread_wqthread + 1129

27  libsystem_pthread.dylib             0x00000001079d0341 start_wqthread + 13

)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb)



[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.

요녀석이 에러의 주범인듯 싶다.


누군가는 이글에서 오류를 해결하기 바라며 일단 오류 메세지 다 올리고


해결은 


통신이 끝나고 화면전환이 이루어지는 부분에


dispatch_async(dispatch_get_main_queue()) {

  self.LoginSuccess()

   }


요로케 해주니 오류 안나더라


그러고 더 작업하는데


로그인 실패시 경고메세지 띄워주는데


또 오류.


그리고 같은 방법으로 해결


------------------------------------------------------------------------------------


dispatch_async(dispatch_get_main_queue())


해결한 후에 저게 뭔지 찾아보니


읽어보니 음 이런거구나


원리는 여기서 읽어보시고


해결해서 기쁨.

'COMPUTER > swift' 카테고리의 다른 글

uitableview 기타등등 기능.  (0) 2016.09.20
swift tab  (0) 2016.09.19
swift 간단 animation 효과  (0) 2016.08.10
swift에서 web을 통해 json 받아오기  (1) 2016.07.20
스위프트 기초  (0) 2016.02.16

+ Recent posts