Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-08-2011, 06:18 PM   #1 (permalink)
pdd
Registered Member
 
Join Date: Jun 2011
Posts: 5
pdd is on a distinguished road
Default Asynchronous URLRequest methods within the same class...

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!
pdd is offline   Reply With Quote
Old 09-08-2011, 06:35 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Don't call them all in a loop. Call the webservice only for the first item in the array, then when it completes (in delegate methods), remove that item from the array, and if there are other items still in it, call the service for the next one. That way it will loop through them all one by one.
baja_yu is offline   Reply With Quote
Old 09-09-2011, 10:54 AM   #3 (permalink)
pdd
Registered Member
 
Join Date: Jun 2011
Posts: 5
pdd is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Don't call them all in a loop. Call the webservice only for the first item in the array, then when it completes (in delegate methods), remove that item from the array, and if there are other items still in it, call the service for the next one. That way it will loop through them all one by one.
I implemented that this morning, and it works great.

But, is there a better way to make the method execute synchronously? NSOperationQueue or something like that?
pdd is offline   Reply With Quote
Old 09-09-2011, 12:07 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You could try that as well, Loading… see the setMaxConcurrentOperationCount to set how many operations can run at the same time. It notes:

Quote:
Note: Setting the maximum number of operations to 1 effectively creates a serial queue for processing operations.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 387
9 members and 378 guests
apatsufas, Eclectic, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:56 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0