Running one function after another completes

后端 未结 4 1397
余生分开走
余生分开走 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 11:51

    What you need is a completion handler with a completion block.

    Its really simple to create one:

    func firstTask(completion: (success: Bool) -> Void) {
        // Do something
    
        // Call completion, when finished, success or faliure
        completion(success: true)
    }
    

    And use your completion block like this:

    firstTask { (success) -> Void in
        if success {
           // do second task if success
           secondTask()
        }
    }
    
    0 讨论(0)
  • 2020-12-14 11:54

    You can achieve like this :-

    func demo(completion: (success: Bool) -> Void) {
         // code goes here
       completion(success: true)
    }
    
    0 讨论(0)
  • 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)
                        })
    
    0 讨论(0)
  • 2020-12-14 12:15
      function1();
      function2();
    

    Use functions!! Once function1() function completed, function2() will execute.

    0 讨论(0)
提交回复
热议问题