簡體   English   中英

我的警報彈出窗口崩潰我的應用程序 - iOS

[英]My Alert popup crashes my app - iOS

我正在為我的iOS應用程序做一個注冊頁面。 我知道我的服務器端代碼很好。 當用戶成功登錄警報時,它會觸發我的應用程序崩潰。 如果我刪除警報然后我的應用程序是好的(我知道這是因為我使用控制台打印'成功')。 請問有人可以告知我的警報導致此次崩潰的原因嗎?

 .....
 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()

我的提醒功能

 // 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)
}

安慰:

    [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/myphpFile.php?userEmail=e@gm.com&firstname=Tim&lastname=t&userPassword=bab
jsonObject: {
    email = "e@gm.com";
    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) 

閱讀錯誤原因,它意味着解決方案:

...可能只能從主線程中調用

DispatchQueue.main.async {
    self.displayAlertMessage(msg: message)
}

[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished]只能從主線程調用。

Ans:不是主線程的線程(來自網絡請求中的回調)。 解決方案是調度您的調用以將視圖控制器呈現給主線程。在主線程中顯示您的警報,

DispatchQueue.main.async(execute: {() -> Void in
        //Code that presents or dismisses a view controller here
        self.present(alert, animated: true, completion: nil)
    })

或者在這里更新主線程中的視圖

   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)
    })
  }

首先,您應該在主線程上顯示警報消息。

此外,捕獲對控制器的引用可能很有用。

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM