0

How can I wait for some method to complete and then continue work ??

- (void)loadMoreDataOnBottomOfTableview
{
NSLog(@"LOADING ITEMS ON BOTTOM");
[self refreshStream];

[self.mainTableView reloadData];

...
}

So I need to wait refreshStream method to complete and then reload tableview data and rest of loadMoreDataOnBottomOfTableview (...).

tshepang
  • 12,111
  • 21
  • 91
  • 136
zhuber
  • 5,364
  • 3
  • 30
  • 63

5 Answers5

1

Use a completion block. That's what they were designed for.

See the completion handler section in this guide. http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html

Victor Engel
  • 2,037
  • 2
  • 25
  • 46
1

Redefine refreshStream

 -(void)refreshStream:(void (^)(void))complete;

 -(void)loadMoreDataOnBottomOfTableview
 {
     [self refreshStream:^{
           [self.mainTableView reloadData];
     }];
 }

This should do you right also check out this page, using typedef is the unspoken standard.

http://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/_index.html

Ben Coffman
  • 1,750
  • 1
  • 18
  • 26
0
[self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>];

You can call your method using this.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Himanshu
  • 421
  • 3
  • 10
0

I can answer your query in swift. Similarly you can use completion block in your code to achieve the task.

class TestClosure
{

func calculateAdditionData() {

    countNumbers({ (result) -> Void in

        println("[self refreshStream] completed")
        //[self.mainTableView reloadData];
    })
}


func refreshStream(completion: (() -> Void)!) {

    //Your refresh code
    completion()
}

}

Completion blocks/Closures are a proper way to wait for something to complete.

sudhanshu-shishodia
  • 1,100
  • 1
  • 11
  • 25
-1

You can use performSelectorOnMainThread:withObject:waitUntilDone: as follows:

 [self performSelectorOnMainThread:@selector(refreshStream) withObject:nil waitUntilDone:YES]

 [self.mainTableView reloadData];

Note however that this is NOT a recommended design pattern. Async calls should use callbacks (your refreshStream method should call back to a method in your view controller which should then trigger reloadData

Jai Govindani
  • 3,181
  • 21
  • 26