Fast function execution after anther function termination

Hi everyone, I'm new to Swift, and I wrote two functions (just a multi-choice quiz app)

func createQuestions() // this goes to Parse, and fetches the questions data that are going to question the users and store them into local arrays

func newQuestion() // this also fetches some other data (for example, some incorrect choices) from Parse and read local variables and finally set the labels to correctly display to the users

I want to ViewDidLoad, first run createQuestion(), after it is fully completed, run newQuestion(). Otherwise, it newQuestion()has some problems reading from local variables that should have been extracted. How will I succeed? Thanks

EDIT: Thank you very much! I learned to use closure! Another follow-up question. I use the for loop to create questions. However, the problem is that the for loop does not execute in an orderly manner. Then my check for the re-function (vocabTestedIndices) fails, and this leads to two identical questions. I want the for loop to run one after another, so the questions you create will not overlap. Thanks! code image

+4
source share
4 answers

try

override func viewDidLoad() {
    super.viewDidLoad()
    self.createQuestions { () -> () in
        self.newQuestion()
    }
}


func createQuestions(handleComplete:(()->())){
    // do something
    handleComplete() // call it when finished s.t what you want 
}
func newQuestion(){
    // do s.t
}
+7
source

How about a quick one deferfrom this post ?

func deferExample() {
    defer {
        print("Leaving scope, time to cleanup!")
    }
    print("Performing some operation...")
}

// Prints:
// Performing some operation...
// Leaving scope, time to cleanup!
+4
source

. , , . ( @Paulw11 ) viewDidLoad:

self.createQuestions()

The task you want to complete depends on Parse's answer:
only after receiving the answer do you want to call the newQuestion function.

Here is the summary documentation for a quick one: https://www.parse.com/docs/ios/guide#objects-retrieving-objects

func createQuestions() {
    var query = PFQuery(className:"GameScore")
    query.whereKey("playerName", equalTo:"Sean Plott")
    query.findObjectsInBackgroundWithBlock {
      (objects: [PFObject]?, error: NSError?) -> Void in

      if error == nil {
        // The find succeeded.

        self.newQuestion()        
      } else {
        // Log details of the failure
        print("Error: \(error!) \(error!.userInfo)")
      }
    }
}

func newQuestion() {
 //here is your code for new question function
}
0
source

Closing will help you achieve this functionality.
Create your createQuestions function as shown below.

func createQuestions(completion:((Array<String>) -> ())){

    //Create your local array for storing questions
    var arrayOfQuestions:Array<String> = []

    //Fetch questions from parse and allocate your local array.
    arrayOfQuestions.append("Question1")

    //Send back your questions array to completion of this closure method with the result of question array.
    //You will get this questions array in your viewDidLoad method, from where you called this createQuestions closure method.
    completion(arrayOfQuestions)
}  

viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        //Calling createQuestions closure method.
        self.createQuestions { (arrayOfQuestions) -> () in

            //Pass your local questions array you received from createQuestions to newQuestion method.
            self.newQuestion(arrayOfQuestions)
        }
    }  

New Question Method

func newQuestion(arrayOfQuestions:Array<String>){

        //You can check your questions array here and process on it according to your requirement.
        print(arrayOfQuestions)
    }
0
source

Source: https://habr.com/ru/post/1625772/


All Articles