my app is using network connections as in here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
.
.
.
receivedData = [[NSMutableData alloc] init];
}
.
-(void) dataFromWeb{
request = [[NSURLRequest alloc] initWithURL: url];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
NSLog(@"** NSURL request sent !");
} else {
NSLog(@"Connection failed");
}
.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
.
.
.
[theConnection release];
[request release];
// Here - using receivedData
[receivedData release];
}
.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
.
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[theConnection release];
[request release];
[receivedData release];
}
.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
. Down the road in the app is this snippet (onButtonPressed):
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Connection failed");
}
What I want to understand is:
-
If I wanted to create another simultaneous URLRequest, would I need to use a different connection so that data arriving from the web won’t mix up upon retrieval?
-
In this code, what happens is that sometimes the code would crash on the line of the function didReceiveResponse()
setLength:0, when the app crashed I sawreceivedData=nilshould I change the line toif(receivedData!=nil) [receivedData setLength:0]; else receivedData = [[NSMutableData data] retain]; -
I am not so sure what this line is doing receivedData = [[NSMutableData data] retain];
I think that the easiest way to handle 2 connections is duplicating the
NSMutableData. I use in that way, and it works perfectly for me.Fist you do this in the point you want the second connection:
And then, in the method you ask for receivedData2 or receivedData
And when you use it don´t forget to do: