2

Note: This similar SO question talks about how to build the same class, but doesn't address my memory management issues related to using the class.

I would like to use sendAsynchronousRequest:queue:completionHandler:, however I need to support iOS 4.2. Thus, I created a custom class called JCURLRequest that uses NSURLConnection and generates an nice interface:

- (void)sendRequest:(NSURLRequest *)request 
    responseHandler:(JCURLResponseHandler)responseHandler;

I have a question about using this class (I'm in ARC memory management):

  • When I create my JCURLRequest object do i need to retain a reference to that object? Or can I just "fire and forget it"?

Note: I understand the basics of ARC - if you have a pointer to an object, it will be kept alive, if there are no more pointers to an object, it will be release in the next auto-release pool.

Thus, I want to know - can I call it like (1) or do i need to use (2)

(1)

JCURLRequest *jcURLRequest = [[JCURLRequest alloc] init];
[jcURLRequest sendRequest:myRequest
          responseHandler:^(NSData *data, NSError *error) { ... }];
// Assuming I don't maintain a reference to jcURLRequest after this

(2)

// Assume @property (strong) JCURLRequest *jcURLRequest;
//        @synthesize jcURLRequest = _jcURLRequest;
self.jcURLRequest = [[JCURLRequest alloc] init];   
[self.jcURLRequest sendRequest:myRequest
          responseHandler:^(NSData *data, NSError *error) { ... }];

NSURLConnection uses an asynchronous callback, so my thought is that I have to use (2). This is because - by the time the delegate callback "calls back", the jcURLRequest instance could have been cleaned up in an auto-release pool.

I'm confused though, because I've tested with (1) and it "seems" to work fine. But, my thought is that maybe it's just coincidence that it's working- ie. really there are no more valid pointers to the jcURLRequest object but the iOS just hasn't gotten around to deallocating it.

Below is the full JCURLRequest class for reference

//  JCURLRequest.h
#import <Foundation/Foundation.h>

typedef void (^JCURLResponseHandler) (NSData *data, NSError *error);

@interface JCURLRequest : NSObject
- (void)sendRequest:(NSURLRequest *)request responseHandler:(JCURLResponseHandler)responseHandler;
@end



//  JCURLRequest.m
#import "JCURLRequest.h"

@interface JCURLRequest ()
{
    JCURLResponseHandler responseHandler;
}
@property (strong, nonatomic) NSMutableData *responseData;
@end

@implementation JCURLRequest
@synthesize responseData = _responseData;


#pragma mark - Public API

- (void)sendRequest:(NSURLRequest *)request responseHandler:(JCURLResponseHandler)handler
{
    responseHandler = [handler copy];
    dispatch_async(dispatch_get_main_queue(), ^{
        __unused NSURLConnection *connectionNotNeeded = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    });
}


#pragma mark - Private API

- (NSMutableData *)responseData
{
    if (!_responseData) 
    {
        _responseData = _responseData = [[NSMutableData alloc] initWithLength:0];
    }

    return _responseData;
}


#pragma mark - URL Connection Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    responseHandler(nil, error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{    
    responseHandler([NSData dataWithData:self.responseData], nil);
}

@end
Community
  • 1
  • 1
bearMountain
  • 3,950
  • 1
  • 36
  • 44

2 Answers2

2

I would recommend retaining JCURLRequest to be safe and correct. Take the following code for example:

@interface AsyncObject : NSObject
{
    BOOL ivar;
}
-(void)sendRequest:(void(^)()) callback;
@end

@implementation AsyncObject
-(void)sendRequest:(void (^)())callback
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Starting Long Async Task");
        sleep(15);
        //ivar = YES;
        NSLog(@"Calling Callback");
        callback();
    });
}
-(void)dealloc
{
    NSLog(@"Deallocated!");
}
@end

    ...
    [[[AsyncObject alloc] init] sendRequest:^{ NSLog(@"Called Back"); }];

If you leave ivar commented out the first thing that prints out is Deallocated! which means the AsyncObject gets deallocated before the request even finishes. Now if you uncomment ivar then Deallocated! is now last as it should be since the block properly retains the correct values. This could, however, be broken if the dispatch used a _weak reference to self.

__weak AsyncObject *self_ = self;
    ... //Inside gcd async method
    self_->ivar = YES; //This will break
Joe
  • 56,979
  • 9
  • 128
  • 135
  • Thank you for your answer. It turns out, as joerick pointed out, that `NSURLConnection` retains its delegate. But, your answer still gave me great new insight. It's very useful to use `-(void)dealloc` to track the lifecycle of an object if I'm unsure about it. I think once I converted to ARC I abandoned all `retain`,`release`,`dealloc`, but it's very useful to remember that I can still implement `dealloc`! – bearMountain Apr 24 '12 at 17:13
2

NSURLConnection retains its delegate, as well as itself while the connection is alive. So it's no coincidence that your solution works.

I can't find any official source on this, but it's been confirmed unofficially here on SO:

Community
  • 1
  • 1
joerick
  • 16,078
  • 4
  • 53
  • 57
  • Didn't know that about NSURLConnection - thanks, that's so helpful. I found that under [special considerations](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/uid/20001697-BAJDDIDG) you'll see the official documentation of this. About the `__unused`, I use that to suppress the warning `Expression result unused`. Do you know another way to suppress that warning? – bearMountain Apr 24 '12 at 16:54
  • Also referencing `self` inside the block causes it to be retained. – Joe Apr 24 '12 at 17:19
  • @bearMountain The other way to suppress the warning is to not store the expression result! (as I have shown above). If you're not going to use the variable, there's no need to store it. – joerick Apr 25 '12 at 08:36
  • @Joe, yes, but this is a short-lived dispatch to the main thread, this block will not be alive for the duration of the connection (it's an asynchronous API). – joerick Apr 25 '12 at 08:37
  • @joerick In my Xcode, at least, the way you wrote it -will- generate the warning. The warning comes from not using the result of the `init` expression. -try it – bearMountain Apr 25 '12 at 14:43
  • @bearMountain Interesting! I did not know that. I removed my nitpicking from the answer! – joerick Apr 25 '12 at 21:12