1

I have 2 functions (func A() and func B()). I want to know how to make function B wait for function A to execute before proceeding. I've done a lot of reading, and know we can somehow do it with asynchronous programming and semaphores, or with functions such as dispatch_barrier_sync. However, I'm really new to swift, and am not understanding how to code this. I would really appreciate it if someone could help.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
A()
B()

}
4
  • What are you doing in function a can you share it? Commented May 26, 2015 at 6:47
  • In function A and B both, I'm making queries to my Parse database Commented May 26, 2015 at 6:48
  • Parse has methods that include callbacks for when the operation has completed. You're better off calling your functions from there. Commented May 26, 2015 at 6:51
  • Could you direct me to those methods and how to use them for this scenario? I couldn't find them in the parse documentation. Commented May 26, 2015 at 6:55

2 Answers 2

1
func a(completion: ()->Void){
  let query:PFQuery = PFQuery(className: "GameScore")
  query.getObjectInBackgroundWithId("xWMyZ4YEGZ", block: { (object, error) -> Void in
    if let object = object{
      completion()
    }
  })
}

func b(){
  //get your other object
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  a(){ () -> Void in
    //Call B when A is completed
    b()
  }
}

Here we add a completion block to function A.
We call this block when the object has been returned.
When we call function A we use the { () -> Void in ……} to execute code only when function A has been completed.

(Code after abhishekkarwar)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response! Where do I add in the rest of my code for the query in function A? I'm takling about: if error == nil { for object in objects {
also I'm using findObjectsInBackgroundWithBlock so would my code just look like : findObjectsInBackgroundWithBlock(block: { (object, error) -> Void in if let object = object{ completion() } })
0

// Please convert bellow code in swift

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 [self functionAWithCompletion:^(){
    //After completion of a cal b function
   [self functionB];
}];

}

- (void)functionAWithCompletion:(void (^)())completion{

PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query getObjectInBackgroundWithId:@"xWMyZ4YEGZ" block:^(PFObject *gameScore, NSError *error) {

     completion();
}];

}

- (void)functionb{


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.