Reputation: 173
I am doing a registration page for me iOS app. I know that my server side code is fine. When the user has successfully signed in the alert that it triggers crashes my app. If i remove the alert then my app is fine (I know this because i use the console to print out 'success'). Please can someone advise on why my alert causes this crash?
.....
let task = session.dataTask(with: request) { (data, response, error) in
// When request is complete, run code below
if let jsonData = data{
do{
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
print("jsonObject: \(jsonObject)")
guard
let myArray = jsonObject as? [String:Any] else{
print("error-2")
return
}
if let status = myArray["status"] as? Int{
// Communication with server successful
if(status == 200){
// Registration successful
if let message = myArray["message"] as? String{
self.displayAlertMessage(msg: message)
}
}
}
}catch let error{
print("print error: \(error)")
}
}else if let requestError = error{
print("error detail: \(requestError)")
}else{
print("unexpected error")
}
}
task.resume()
My alert function
// General Alert message function
func displayAlertMessage(msg: String){
let alert = UIAlertController.init(title: "Alert", message: msg, preferredStyle: .alert)
let userAction = UIAlertAction.init(title: "OK", style: .destructive, handler: nil)
alert.addAction(userAction)
present(alert, animated: true, completion: nil)
}
CONSOLE:
[MobileAssetError:29] Unable to copy asset information from https://mesu.apple.com/assets/ for asset type com.apple.MobileAsset.TextInput.SpellChecker
url: https://www.myDomaincom/myFolder/[email protected]&firstname=Tim&lastname=t&userPassword=bab
jsonObject: {
email = "[email protected]";
firstname = Tim;
lastname = t;
message = "Registration success";
status = 200;
userId = 32;
}
2017-04-29 12:33:57.042 lmyApp[60847:11563379] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/Keyboard/UIKeyboardTaskQueue.m:432
2017-04-29 12:33:57.057 myApp[60847:11563379] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109891d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000106ba421e objc_exception_throw + 48
2 CoreFoundation 0x0000000109895e42 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010673966d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000107bc4b65 -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 165
5 UIKit 0x0000000107335441 -[UIKeyboardImpl setDelegate:force:] + 1404
6 UIKit 0x0000000107755dde -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 981
7 UIKit 0x000000010775f5f8 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 498
8 UIKit 0x0000000107227543 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1178
9 UIKit 0x000000010722928e -[UIViewController _presentViewController:withAnimationController:completion:] + 4971
10 UIKit 0x000000010722c26b -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530
11 UIKit 0x000000010722bd51 -[UIViewController presentViewController:animated:completion:] + 179
12 lifesci-PubMed 0x00000001065b3fd2 _TFC14lifesci_PubMed22RegisterViewController19displayAlertMessagefT3msgSS_T_ + 834
13 lifesci-PubMed 0x00000001065b3378 _TFFC14lifesci_PubMed22RegisterViewController24submitRegistrationTappedFP_T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_ + 2088
14 lifesci-PubMed 0x00000001065b3bcb _TTRXFo_oGSqV10Foundation4Data_oGSqCSo11URLResponse_oGSqPs5Error____XFdCb_dGSqCSo6NSData_dGSqS1__dGSqCSo7NSError___ + 203
15 CFNetwork 0x0000000109cafccc __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
16 CFNetwork 0x0000000109caf578 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 308
17 Foundation 0x00000001066a69ad __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
18 Foundation 0x00000001066a668f -[NSBlockOperation main] + 101
19 Foundation 0x00000001066a4d8c -[__NSOperationInternal _start:] + 672
20 Foundation 0x00000001066a0ccf __NSOQSchedule_f + 201
21 libdispatch.dylib 0x000000010a7f50cd _dispatch_client_callout + 8
22 libdispatch.dylib 0x000000010a7d2e17 _dispatch_queue_serial_drain + 236
23 libdispatch.dylib 0x000000010a7d3b4b _dispatch_queue_invoke + 1073
24 libdispatch.dylib 0x000000010a7d6385 _dispatch_root_queue_drain + 720
25 libdispatch.dylib 0x000000010a7d6059 _dispatch_worker_thread3 + 123
26 libsystem_pthread.dylib 0x000000010aba4712 _pthread_wqthread + 1299
27 libsystem_pthread.dylib 0x000000010aba41ed start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Upvotes: 1
Views: 1429
Reputation: 5679
First of all, you should display your alert message on main thread.
Also, it might be useful to capture a weak reference to your controller.
final class SomeController: UIViewController {
func sendRequest() {
let message = "your message"
DispatchQueue.main.async { [weak self] in
self?.displayAlertMessage(msg: message)
}
}
func displayAlertMessage(msg: String){
// present
}
}
Upvotes: 0
Reputation: 285072
Read the error reason, it implies the solution:
... may only be called from the main thread
DispatchQueue.main.async {
self.displayAlertMessage(msg: message)
}
Upvotes: 2
Reputation: 82759
[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.
Ans : thread that was not the main thread (from a callback in a network request). The solution is to dispatch your calls to present the view controllers to the main thread.present your alert in main thread,
DispatchQueue.main.async(execute: {() -> Void in
//Code that presents or dismisses a view controller here
self.present(alert, animated: true, completion: nil)
})
or else update your view in main thread in here
if let message = myArray["message"] as? String{
DispatchQueue.main.async(execute: {() -> Void in
//Code that presents or dismisses a view controller here
self.displayAlertMessage(msg: message)
})
}
Upvotes: 1