ios - Swift 错误线程(一些数字): signal SIGABRT

标签 ios swift

有时,当用户按下按钮但错误代码并不总是相同时,我的应用程序会因出现此异常而自行终止。有时是 Thread 11: signal SIGABRT 有时是 Thread 13: signal SIGABRT

这个错误的主要问题是什么?

    @IBAction func emailchk(_ sender: Any) {
    if email.text == ""  {
        let messagebox = UIAlertController(title: "PSM Passport | Reset Password",message: "กรุณากรอก อีเมลที่คุณใช้เปิดบัญชี เพื่อทำการเปลี่ยนรหัสผ่าน",preferredStyle: UIAlertControllerStyle.alert)
        messagebox.addAction(UIAlertAction(title: "OK", style: .default ))
        present(messagebox, animated: true, completion: nil)


    } else {

        let logginbox = UIAlertController(title: "PSM Passport | Checking", message:
            "กำลังตรวจสอบบัญชีที่คุณระบุมาอยู่ กรุณารอซักครู่", preferredStyle: UIAlertControllerStyle.alert)


        self.present(logginbox, animated: true, completion: nil)


        let request = NSMutableURLRequest(url: NSURL(string: "some url that linked to the php file in my server")! as URL)
        request.httpMethod = "POST"

        var postString = "username=\(email.text!)"
        request.httpBody = postString.data(using: String.Encoding.utf8)
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in

            if error != nil {
                logginbox.dismiss(animated: true){
                    let alertController = UIAlertController(title: "PSM Passport | ไม่สามารถติดต่อกับ Server ได้", message:
                        "ระบบไม่สามารถทำการติดต่อกับ Server ได้ กรุณาตรวจสอบการเชื่อมต่อของคุณเเล้วลองใหม่อีกครั้ง หากปัญหานี้ยังคงเกิดขึ้นเรื่อยๆ กรุณาติดต่อ SCI-Code Team เพื่อดำเนินการเเก้ไขปัญหาต่อไป", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default,handler: nil))

                    self.present(alertController, animated: true, completion: nil)
                }
                return
            }



            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            var a = responseString as! String

            if a == "notfround\n" {
                logginbox.dismiss(animated: true) {
                    let errormsg = UIAlertController(title: "PSM Passport | Reset Password", message:
                        "ระบบไม่พบบัญชี PSM Passport นี้ในระบบ กรุณาตรวจสอบข้อมูลให้ถูกต้องเเล้วลองใหม่อีกครั้ง", preferredStyle: UIAlertControllerStyle.alert)
                    errormsg.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default,handler: nil))

                    self.present(errormsg, animated: true, completion: nil)
                }




            } else {
                var usre = self.email.text
                UserDefaults.standard.set(usre,forKey: "usernametoreset")
                UserDefaults.standard.set(a, forKey: "UIDtoreset")

                logginbox.dismiss(animated: true) {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let otpreset = storyboard.instantiateViewController(withIdentifier: "otpreset") as! UIViewController



                    self.navigationController?.pushViewController(otpreset, animated: true)


                }

                //print("\(responseString)")

            }
        }
        task.resume()


    }
}

应用日志

2018-01-27 18:37:20.751198+0700 PSMApplication[5108:1668568] [MC] Lazy loading NSBundle MobileCoreServices.framework
2018-01-27 18:37:20.751666+0700 PSMApplication[5108:1668568] [MC] Loaded MobileCoreServices.framework
2018-01-27 18:37:23.564509+0700 PSMApplication[5108:1668568] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-01-27 18:37:23.567171+0700 PSMApplication[5108:1668568] [MC] Reading from public effective user settings.
2018-01-27 18:37:41.568670+0700 PSMApplication[5108:1669041] *** Assertion failure in -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.34.4/UIApplication.m:1709
2018-01-27 18:37:41.571500+0700 PSMApplication[5108:1669041] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'accessing _cachedSystemAnimationFence requires the main thread'
*** First throw call stack:
(0x1823a3164 0x1815ec528 0x1823a3038 0x182d3d7f4 0x18bbbc758 0x18bbbc784 0x18bc19e24 0x18be52d34 0x18c22c0d8 0x18ba11a48 0x1a160f758 0x18ba2d610 0x18bcdaf9c 0x18bca92e8 0x18bca71e8 0x18bcdaa2c 0x18ba739a8 0x18ba1f0a0 0x1a15deb0c 0x18ba1ee7c 0x1a15deb0c 0x18bcd9ca0 0x18ba1ebec 0x1a15de5bc 0x100de44ec 0x100dd3eb4 0x18292dd68 0x1829466bc 0x182d79ba0 0x182cb9894 0x182ca94c4 0x101c6528c 0x101c719e4 0x101c6528c 0x101c719e4 0x101c718a4 0x182d7b878 0x101c6528c 0x101c72678 0x101c70f08 0x101c769d0 0x101c766f4 0x181fcb06c 0x181fcab6c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

最佳答案

Mainthread 问题 dismiss 和 present 之间的交互? URLSession 上的完成 block ;)

DispatchQueue.main.async {
    logginbox.dismiss(animated: true) {
        let alertController = UIAlertController(...)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default,handler: nil))

         DispatchQueue.main.async {
            self.present(alertController, animated: true, completion: nil)
         }
    }
}

弱者请自行处理;)

关于ios - Swift 错误线程(一些数字): signal SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475516/

相关文章:

node.js - 使用 socket.io 和 Swift 3 等待消息

generics - 如何从泛型函数中调用最精确的重载?

ios - `UTF16Index()` 或 `UTF16Index.init(encodedOffset:)` 用于 Xcode 8/9 支持

ios - Swift Metal Compute Shaders 提供意想不到的混合结果

ios - 如何限制API调用次数?

ios - 从 Block 获取 WebService 响应到 WatchKit App

ios - 如何创建 cocoapod 库

ios - 带有 IBOutlet 对象而不是 IBOutlet 对象的 Xcode 自定义单元格

objective-c - 符号 ^ 在 Objective-C 中代表什么?

iOS UIImagePickerController 和 TakePicture 问题