警报控制器在主线程上冻结

时间:2016-12-16 13:04:55

标签: swift xcode uialertcontroller nsurlsessiondatatask

连接到服务并从后端接收数据。代码如下:

 _ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in

        if response != nil{

           var responseREcvd = response as? NSHTTPURLResponse
            if responseREcvd?.statusCode == 404 {
                let  alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert)
                let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                alertControler.addAction(alertAction)
                dispatch_async(dispatch_get_main_queue(), {
                    targetVC.presentViewController(alertControler, animated: true, completion: nil)
                })
            }
        }

显示警报但会冻结用户界面。当我按下确定操作按钮时,不会调用任何操作。

2 个答案:

答案 0 :(得分:0)

试试此扩展程序

$tempdate = 'Di, 02.Okt 2012';
$tempdate = str_replace('Di,', '', $tempdate);
$tempdate = str_replace('.Okt ', '-10-', $tempdate);
$tempdate = date('Y-m-d', strtotime($tempdate));
echo $tempdate;

// output: "2012-10-02"

使用:

  extension UIAlertController {

    func show() {
        present(true, completion: nil)
    }

    func present(
        animated: Bool, completion: (() -> Void)?) {
        if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
            presentFromController(rootVC, animated: animated, completion: completion)
        }
    }

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
        self.addAction(UIAlertAction.init(title: "Ok", style: .Default, handler: { (true) in
            self.dismissViewControllerAnimated(true, completion: nil)
        }))
        if let navVC = controller as? UINavigationController,
            let visibleVC = navVC.visibleViewController {
            presentFromController(visibleVC, animated: animated, completion: completion)
        } else
            if let tabVC = controller as? UITabBarController,
                let selectedVC = tabVC.selectedViewController {
                presentFromController(selectedVC, animated: animated, completion: completion)
            } else {
                controller.presentViewController(self, animated: animated, completion: completion);
        }
    }
}

希望这对你有用

答案 1 :(得分:0)

尝试使用NSOperationQueue:

 _ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in

    if response != nil{

       var responseREcvd = response as? NSHTTPURLResponse
        if responseREcvd?.statusCode == 404 {
            let  alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert)
            let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertControler.addAction(alertAction)
            dispatch_async(dispatch_get_main_queue(), {NSOperationQueue.mainQueue().addOperationWithBlock {
                targetVC.presentViewController(alertControler, animated: true, completion: nil)}
            })
        }
    }