I have two methods. I want to execute one after finishing task of first one. How can i do this?
Asked
Active
Viewed 1,336 times
2
-
1put some code brother – Prashant Tukadiya Jan 05 '17 at 11:22
-
Call your second method the line before returning first method completion handler – Sivajee Battina Jan 05 '17 at 11:24
-
@SivajeeBattina please show me some code, i am weak in completion handler. – Saad Jan 05 '17 at 11:26
-
func doSomething(flag:Bool, completionHandler:(success:Bool) -> Void) { //Here you need to call your second method self.secondMethod() completionHandler(success:true) } func secondMethod(){ } – Sivajee Battina Jan 05 '17 at 11:31
2 Answers
7
I assume you are looking for simple completion block solution, so this should be sufficient.
-(void)method1:(void (^ __nullable)(void))completion {
NSLog(@"method1 started");
//Do some stuff, then completion
completion();
NSLog(@"method1 ended");
}
-(void)method2{
NSLog(@"method2 called");
}
Use like this,
- (void)viewDidLoad{
[super viewDidLoad];
[self method1:^{ //After method1 completion, method2 will be called
[self method2];
}];
}

Mohammad Zaid Pathan
- 16,304
- 7
- 99
- 130
1
you can do something like,
[[manager POST:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"This is success!!!");
//this is first method's completion blcok!
// this is another method from completion of first
[self saveImages:^(BOOL isDone) {
// this is second method's completion
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"failure : Error is : %@",error.localizedDescription);
// this is completion of first method but with failure
}]resume];
This is the simple example that how to manage it!! In this I have used AFNEtworking's method, so don't confuse with it!

Ketan Parmar
- 27,092
- 9
- 50
- 75