在异步完成处理程序中关闭警报抛出异常

Dismissin Alert in async complection Handler throws exception

我有以下代码在其完成处理程序中调用异步方法,当我尝试解除警报时它崩溃了。

Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView *, NSTextContainer *, NSUInteger)(), /SourceCache/UIFoundation_Sim/UIFoundation-376.14/UIFoundation/TextSystem/NSLayoutManager_Private.m:1547

    var alert1:UIAlertController = UIAlertController(title: "Data loaded", message: "all the data has been loaded", preferredStyle: UIAlertControllerStyle.Alert)
    alert1.addAction(UIAlertAction(title: "dismis me", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alert1, animated: true, completion: nil)

    mywebapi.postAsync("account/Login", token: nil, content: postData)
    {(succeeded,data:NSDictionary!)->() in

    if(succeeded)
    {
        //Crashes at below line
        alert1.dismissViewControllerAnimated(true, completion: nil)


        }
 } 

确保在主线程上调用完成处理程序。所有 UI 相关的操作都必须在主线程上执行。您可以使用 dispatch_async:

包装呼叫
dispatch_async(dispatch_get_main_queue(), ^{
    alert1.dismissViewControllerAnimated(true, completion: nil)
});