2

I have two methods. I want to execute one after finishing task of first one. How can i do this?

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
Saad
  • 235
  • 2
  • 12

2 Answers2

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