so I got class:
DataFetcher.h
#import <Foundation/Foundation.h>
@interface DataFetcher : NSObject
{
NSURLConnection *networkConnection;
NSMutableData *recievedData;
BOOL isFetchingFinished;
}
@property (nonatomic, retain) NSMutableData *recievedData;
@property (nonatomic, retain) NSURLConnection *networkConnection;
- (void)fetchDataWithRequest:(NSMutableURLRequest *)request;
DataFetcher.m:
#import "DataFetcher.h"
@implementation DataFetcher
@synthesize recievedData;
@synthesize networkConnection;
- (id)init
{
self = [super init];
if (self) {
NSMutableData *data = [[NSMutableData alloc] initWithLength:0];
self.recievedData = data;
[data release];
}
return self;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection Error:%@\n Failure Reason:%@", [error localizedDescription], [error localizedFailureReason]);
[networkConnection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.recievedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[networkConnection release];
NSLog(@"%@", self.recievedData.length);
[self processData];
}
- (void)fetchDataWithRequest:(NSMutableURLRequest *)request
{
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
self.networkConnection = aConnection;
[aConnection release];
[self.networkConnection start];
if( !self.networkConnection )
{
NSLog(@"Connection failed! recieved nil at alloc point");
}
}
- (void)dealloc
{
[super dealloc];
[recievedData release];
}
@end
Normally it crashes on account of EXC_BAD_ACCESS ( NSZombie wont detect anything ).
I noticed that if you switch on connectionDidFinishLoading
the self.recievedData.length
to just self.recievedData
, it wont crash and looks like there is some data in there. the same thing I get for the self.networkConnection
- if I just print self.networkConnection
, its ok, but if I print self.networkConnection.retainCount
( for example, any ivar will work ) it will crash with the same signal.
I can't seem to grasp where the problem is, and I've been looking for it for quiet a while, And I will appreciate some help with this :)
Tnx!
Oh, and I call it with this code:
DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[fetcher fetchDataWithRequest:request];
[fetcher release];
[request release];