如何在执行后台任务时呈现视图控制器?

How to present a view controller while performing background tasks?

我正在使用 Parse.com 和 swift

我有一个显示 parse.com 登录的初始视图控制器。

登录完成并且对象保存在后台后,我想展示我的导航控制器的根视图控制器(故事板中链接的第一个控制器)。

所有异步调用是如何完成的?

这就是我所拥有的,但它跳回了登录屏幕并且没有

func signUpViewController(signUpController: PFSignUpViewController!, didSignUpUser user: PFUser!) {
 currentUser = user as? User
 currentUser!.isManager = false
 var query = PFUser.query()
 query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
  if objects.count == 1 {
   currentUser!.isManager = true
  }
  currentUser?.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
   if success == false || error != nil {
    println(error)
   } else {
    currentCompany = Company()
    currentCompany!.companyName = "My Company"
    currentCompany!.saveInBackgroundWithBlock({ (success: Bool!, error: NSError!) -> Void in
     if success == false {
      println(error)
     }
    })
   }
  })
 }
 dismissViewControllerAnimated(true, completion: { () -> Void in
  self.performSegueWithIdentifier("jumpFromInitialToMessages", sender: self)
  // let vc = MessagesViewController()
  // self.navigationController?.presentViewController(vc, animated: true, completion: nil)

 })
}

如果你想在完成处理程序中启动一个 UI transition/update,你应该在完成处理程序中插入一个调度返回主队列 关闭:

var query = PFUser.query()
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in

    // do something

    // when done, do some UI update, e.g. perform segue to another scene

    dispatch_async(dispatch_get_main_queue()) {
        self.performSegueWithIdentifier("identifier", sender: self)
    }
}

这是一个带有简单查询的示例,但这个想法适用于任何异步调用的完成处理程序闭包:只需将 UI 更新从闭包内分派回主队列。