Im developing ios application which is getting data from the web server. I want everything else to wait, until one of the handlers of this class is called and completed. I know it is possible by using dispatch/threads, but i just can’t figure out how.
-(void)callWebService:(NSString*)URL:(NSString*)SOAP{
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[SOAP dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con){
[con start];
}
}
and at the end of this method continues code outside this class. but i want to wait until this handler is called (and completed):
-(void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data{
NSString *res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",res);
Ukol_Parser *parser = [Ukol_Parser alloc];
[parser parseUkol:res];
}
because the parser here puts data into sqlite db and outside this class data are being read. But the “outside” code is being executed faster than i get response and handler is called….
If you want “everything else to wait”, then it sounds like what you really want to do are synchronous requests.
Check out [NSURLConnection sendSynchronousRequest:returningResponse:error:]
However, make sure to do this thing on a background thread because if you do it on the main thread, your UI will block and your app will look unresponsive to user touches or anything else.