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

时间:2015-01-12 14:49:29

标签: multithreading swift parse-platform

我正在使用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)

 })
}

1 个答案:

答案 0 :(得分:0)

如果要在完成处理程序内启动UI转换/更新,则应将调度插入主队列里面完成处理程序闭包:

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更新从闭包内部调回主队列。