Running one function after another completes

后端 未结 4 1407
余生分开走
余生分开走 2020-12-14 11:45

I am trying to run loadViews() after the pullData() completes and I am wondering what the best way of doing this is? I would like to set a 10 sec timeout on it as well so I

4条回答
  •  余生分开走
    2020-12-14 12:01

    I had a similar situation where I had to init a view once the data is pulled from Parse server. I used the following:

    func fetchQuestionBank(complete:()->()){
    
            let userDefault = NSUserDefaults.standardUserDefaults()
            let username = userDefault.valueForKey("user_email") as? String
    
            var query = PFQuery(className:"QuestionBank")
            query.whereKey("teacher", equalTo: username!)
    
            query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
    
                if error == nil {
                    if let objects = objects as? [PFObject] {
    
                        var questionTitle:String?
                        var options:NSArray?
    
                        for (index, object) in enumerate(objects) {
    
                            questionTitle = object["question_title"] as? String
                            options = object["options"] as? NSArray
                            var aQuestion = MultipleChoiceQuestion(questionTitle: questionTitle!, options: options!)
                            aQuestion.questionId = object.objectId!
                            InstantlyModel.sharedInstance.questionBank.append(aQuestion)
                        }
    
    
                        complete()
                    }
                }else{
                    println(" Question Bank Error \(error) ")
                }
            }
        }
    

    And this is you call the method:

    self.fetchQuestionBank({ () -> () in
                            //Once all the data pulled from server. Show Teacher View.
                            self.teacherViewController = TeacherViewController(nibName: "TeacherViewController", bundle: nil)
                            self.view.addSubview(self.teacherViewController!.view)
                        })
    

提交回复
热议问题