开发者

How to create something like NSURLConnection?

开发者 https://www.devze.com 2023-01-23 15:34 出处:网络
NSURL *URL = [NSURL URLWithString:@\"http://www.stackoverflow.com\"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *URL = [NSURL URLWithString:@"http://www.stackoverflow.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

With code as simple as that, I can load a webpage in my application. I don't have to worry about retaining or releasing the NSURLConnection, it will autorelease when it's done loading.

I'm creating some sort of wrapper around NSURLConnection, JSONConnection. It allows me to load a JSON value from a webpage and automatically parse that in a NSDictionary. Right now, I have to use it like this:

JSONConnection *tempJSONConnection = [[JSONConnection alloc] initWithURLString:@"http://www.stackoverflow.com" delegate:self];
self.JSONConnection = tempJSONConnection;
[tempJSONConnection release];

Then, when it's done loading, I call self.JSONConnection = nil;.

What I want, is to do this:

JSONConnection *connection = [JSONConnect开发者_如何学JAVAion connectionWithURLString:@"http://www.stackoverflow.com" delegate:self];

I know how to create this method. I just don't know how to keep connection alive when the runloop is finished and the autorelease pool is drained, and make sure connection is deallocated when it's done loading. In other words, I don't how to duplicate the exact behavior of NSURLConnection.


To all intents and purposes, from the outside, NSURLConnection effectively retains itself. This was either done by sending

[self retain];

when starting the connection and then

[self release];

when finished and after informing the delegate; or it was done by placing itself in a pool of currently open connections and removing it from that pool on completion.

You don't actually have to do any of this. NSURLConnection retains its delegate, so your JSON connection class should create an NSURLConnection passing itself as the NSURLConnection's delegate. That way it will live at least as long as the NSURLConnection. It should parse the JSON into a dictionary in the method -connectionDidFinishLoading: and pass the dictionary on to its delegate before returning. After returning the NSURLConnection will release and possibly deallocate itself and also release your JSON connection.


Someone should trac connection's live time in any case. It is a bad solutions to trac it inside the connection.

IMO the right way to do it is use singleton class to perform connections

@protocol JSONDataProviderDelegate <NSObject>
- (void) JSONProvider:(JSONDataProvider*) provider didLoadJSON:(JSONObject*) object;
- (void) JSONProvider:(JSONDataProvider*) provider didFainWithError:(NSError*) error;
@end

@interface JSONDataProvider : NSObject

+ (void) provideJSON:(NSURL*) url delegate:(id<JSONDataProviderDelegate>) delegate;
+ (void) removeDelegate:(id<JSONDataProviderDelegate>delegate);

@end

Usage:

- (void) onSomeEvent
{
  [JSONDataProvider provideJSON:[NSURL URLWithString:@"http://example.com/test.json"] delegate:self];
}

- (void) JSONProvider:(JSONDataProvider*) provider didLoadJSON:(JSONObject*) object
{
  NSLog(@"JSON loaded: %@", object);
}
 - (void) dealloc
{
  [JSONDataProvider removeDelegate:self];
  [super dealloc];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消