だから私はクラスを取得しました:
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
通常、EXC_BAD_ACCESS のアカウントでクラッシュします (NSZombie は何も検出しません)。connectionDidFinishLoading
に切り替えると、クラッシュせず、そこにデータがあるように見えることself.recievedData.length
に気付きました。self.recievedData
私が得たのと同じことself.networkConnection
-印刷するだけself.networkConnection
なら大丈夫ですが、印刷するとself.networkConnection.retainCount
(たとえば、ivarが機能します)、同じ信号でクラッシュします。
問題がどこにあるのか把握できないようで、しばらく静かに探していました。これについての助けに感謝します:)
Tnx!
ああ、私はこのコードでそれを呼び出します:
DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[fetcher fetchDataWithRequest:request];
[fetcher release];
[request release];