Question

   -(void)method1
        {
          [self method2];     
          [self method3]; //After finishing execution of method2 and its delegates I want to execute method3
        }

Here method2 got running when it called but before execution of its delegate methods the method3 got started to execute. How to avoid that? Any suggestion or code please

I called a nsurl connection with its delegates in method 2

 -(void)method2
    {
    ....
       connection= [[NSURLConnection alloc] initWithRequest:req delegate:self ];
    ....
    }


    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

        }

 -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
        {

        }
..
..
Was it helpful?

Solution

Use blocks - it would be easier to handle:

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[[NSOperationQueue alloc] init]

                       completionHandler:^(NSURLResponse *response,
                                           NSData *data,
                                           NSError *error)
 {

     if ([data length] >0 && error == nil) {
         // parse your data here 

         [self method3];

         dispatch_async(dispatch_get_main_queue(), ^{

               // call method on main thread, which can be used to update UI stuffs
               [self updateUIOnMainThread];
         }); 
     }
     else if (error != nil) {
        // show an error
     }
 }];

OTHER TIPS

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) 
{
    [self method3]
}

You are using Asynchronous Url Connection. Thats y method 3 is getting triggered before the method 2 completes. To solve your issue, Use this

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     [self method3];
}

It should definitely work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top