等到委托方法在ios中完成执行

Dol*_*olo 0 multithreading objective-c ios nsurlconnectiondelegate

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

这里,method2在调用时运行,但在执行其委托方法之前,method3开始执行.怎么避免呢?请提出任何建议或代码

我在方法2中调用了与其委托的nsurl连接

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


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

        }

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

        }
..
..
Run Code Online (Sandbox Code Playgroud)

Mru*_*nal 5

使用块 - 它会更容易处理:

[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
     }
 }];
Run Code Online (Sandbox Code Playgroud)