- (id)initWithUrl:(NSURL*)_url withDelegate:(id)del{
if(self = [super init]){
self.theUrl = _url;
self.status = NEW;
self.delegate = del;
self.theData = [NSMutableData data];
}
return self;
}
- (void) startLoadingURL {
self.status = FETCHING;
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
myConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
NSMutableData *data = [NSMutableData data];
[connectionToData setObject:data forKey:myConnection];
[myConnection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[theData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
self.theData = [NSMutableData dataWithData:[connectionToData objectForKey:connection]];
[theData appendData:data];
NSLog([NSString stringWithFormat:@"%@", theData]);[COLOR="Red"]//theData variable is not nil here[/COLOR]
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
@synchronized(self){
if(self.status != FAILED) {
self.status = COMPLETE;
NSMutableData *theDataToSend = [NSMutableData data];
theDataToSend = [connectionToData objectForKey:connection];
NSLog([NSString stringWithFormat:@"%@", theDataToSend]);[COLOR="Red"]//this variable is empty[/COLOR]
[self.delegate startParsing:theDataToSend];
theData = nil;
}
}
[connectionToData removeObjectForKey:connection];
}