异步complection中的Dismissin Alert处理程序抛出异常

时间:2015-09-06 19:13:37

标签: xcode ios8 swift2

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

  

虚假中的断言失败   _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)


        }
 } 

1 个答案:

答案 0 :(得分:1)

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

打包电话
dispatch_async(dispatch_get_main_queue(), ^{
    alert1.dismissViewControllerAnimated(true, completion: nil)
});