I have an application that needs to make a handful of web service calls. Here's how I get to the problem.
First, my app (from a modal view controller) calls a "DataServices" class I created to handle the web service calls. There can be anywhere from one to many web service calls inside the Data Services class.
I have an array of strings representing ID's of objects to be downloaded.
Code:
NSArray *list = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
Then, I loop through this array.
Code:
for (NSString *item in list) {
[self callWebService:item];
}
Then, the callWebService function is called.
Code:
- (void)callWebService:(NSString *)item
{
// Soap message defined properly
NSURL *url = [NSURL URLWithString:webServiceBaseAddress];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *length = [NSString stringWithFormat:@"%d", [soapMessage length]];
[request addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:length forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
webData = [[NSMutableData data] retain];
} else {
NSLog(@"Connection is null.");
}
}
And then I've defined didReceiveResponse, didReceiveData, didFailWithError, and connectionDidFinishLoading.The web service code works fine, because when there is one item in the array, it downloads perfectly. When there is more than one item in the loop, though, the web service call bumps into itself. It's operating asynchronously, and then returning EXC_BAD_ACCESS errors because of the thread conflicts, I'm guessing.
So, is there a way I can make sure the [self callWebService:item] method COMPLETES before moving to the next item in the loop?
Thanks for the help!