Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all.

In my iPhone application, i am making use of

NSURLConnection's (id) initWithRequest: (NSURLRequest*)request delegate: (id)delegate three times sequentially to connect to three xml files.

problem is that i am making use of the info from first URL to connect to second URL and info from second URL to connect to thrid and so on..

But as the above method is parsing in thread-model by default, the second connection is tryiong to establish befor the first one get connected...

All three connection are three separate lines in my program...

How to make the first connection to do things before connecting to second?

I mean how to change the thread beahaviour of the above method??

Thanks
 

wwwww

macrumors newbie
Mar 30, 2009
1
0
Hello,
we are using NSURLConnection to connect web server using
sendSynchronousRequest method.but We are facing so much problem.
1) Request get blocked / Not responding.
2) how to set
Connect Time out : to use for name resolution.
Send Timeout : to use for sending requests
ReceiveTimeout : time is - receive a response to a request.

(NSString*)sendLoadBalenceRequest:(NSString*)XMLString
{
[XMLString retain];
NSLog(@"Sending the Load Balence request to server.");
NSError *myError=nil;
NSHTTPURLResponse *serverResponse=nil;
NSData *serverData;
NSString *serverDataString = nil;
NSDictionary*dic;
NSArray *arr;


@try
{
NSURL*theServerURL = [NSURL
URLWithString:m_strXTCIURL];


NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:theServerURL


cachePolicy:NSURLRequestUseProtocolCachePolicy


timeoutInterval:10.0];
[theRequest setValue:mad:"text/xml"
forHTTPHeaderField:mad:"Content-
Type"];


[theRequest setHTTPMethod:mad:"POST"];


[theRequest setHTTPBody:[XMLString
dataUsingEncoding:NSUTF8StringEncoding]];


NSURLConnection *conn = [NSURLConnection
connectionWithRequest:theRequest delegate:self];


if(conn == nil)
{
NSRunCriticalAlertPanel(NSLocalizedString
(@"Error",NULL),@"Connection can't establish",NSLocalizedString
(@"OK",NULL),NULL,NULL);
return @"";
}
serverData = [NSURLConnection
sendSynchronousRequest:theRequest


returningResponse:&serverResponse


error:&myError];


if(myError!=NULL)
{
NSLog(@"\n\nERROR: %@",[myError
localizedDescription]);
NSLog(@"\n\nERROR code: %d",[myError code]);
NSLog(@"\n\nERROR domain: %@",[myError
domain]);


[self errorHandling:[NSString
stringWithFormat:mad:"%d", [myError
code]] strErrorText:[myError localizedDescription]];


dic = [myError userInfo];
arr = [dic allValues];


int i = 0;
for(i = 0 ; i< [arr count]; i++)
{
NSLog(@"\n%d. %@ \n",i+1,[arr
objectAtIndex:i]);


}
serverDataString = @"";
}
else
{
serverDataString = [[[NSString alloc]
initWithData:serverData
encoding:NSUTF8StringEncoding] retain];
NSLog(@"Server Response :\n%@
\n",serverDataString);
}


} @catch (id e)
{
NSLog(@"EXCEPTION: %@ ",e);
} @finally
{
[XMLString release];
return[serverDataString autorelease];
}



}


Problem::
In this function we have used api sendSynchronousRequest, Which is
blocking if it don't get response, or in case of heavy load, or if
network connection is not proper.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Don't use sendSynchronousRequest. Use the asynchronous methods instead, which will solve your problems.

The timeout is set in NSURLRequest, but it's not guaranteed to work every time (at least I haven't had that experience). You may need to set a timer to cancel.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.