Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:35:06+00:00 2026-06-03T00:35:06+00:00

Note: This similar SO question talks about how to build the same class, but

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:35:08+00:00Added an answer on June 3, 2026 at 12:35 am

    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:

    • been having a little confusion about the retainCount of NSURLConnection

    • Does a NSURLConnection retain its delegate?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This is similar, but not quite the same as this other question I've
[Note: This question is very similar, but not quite the same.] I'm trying to
[NOTE: This questions is similar to but not the same as this one .]
Note this is not a duplicate of this similar but different question ! My
Note: There's a very similar question , but it's WPF-specific; this one is not.
I saw this similar question here but can't figure out how to use Contains
Note this question is similar this one except I'm not working with linq-to-sql, so
Note: This question is similar to How to prevent the copy of XML documentation
Note: I posted a similar question , which was the ancestor of this question,
I'll preface this question with the note that I have looked at this similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.