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

  • SEARCH
  • Home
  • 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 7986463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:51:39+00:00 2026-06-04T11:51:39+00:00

I’ve created a class which is a wrapper around NSURLConnection and users of the

  • 0

I’ve created a class which is a wrapper around NSURLConnection and users of the class pass blocks that get invoked at various stages of the connection. THe wrapper class is shown below, abbreviated for brevity:

typedef void (^ConnectionFinishedWithErrorBlock)(NSError* error);

@interface MYHTTPConnection : NSObject <NSURLConnectionDelegate>
@property (copy, nonatomic)     ConnectionFinishedWithErrorBlock   theErrorBlock;
@property (strong, nonatomic)   NSURLConnection *connection;

- (void) establishConnectionForURL:(NSURL*) url 
      andConnectionFinishedWithErrorBlock: (ConnectionFinishedWithErrorBlock) errorBlock;
@end

So far I’ve been using it with the blocks declared inline i.e.

[self.httpConnection establishConnectionForURL: url
            andConnectionFinishedWithErrorBlock:^(NSError* error)
            {
                ...
            }]; 

But now I have a situation where I have a very long error handling block, and to place all its code inline will get messy (as the API takes other blocks not shown here).

I know I could do something like this:

void (^httpConnectionFinishedWithError)(NSError*) = 
^(NSError* error) 
{
}

Then pass httpConnectionFinishedWithError to establishConnectionForURL. But httpConnectionFinishedWithError contains calls to self. The calls to self are fine when the block is declared inline, but not when done as above as obviously this generates compilation errors.

So I was wondering if/how the block could be made a block property of the calling class? (I’ve already used blocks as properties of classes before, as is done in the MYHTTPConnection class above which works fine, but in this situation I can’t get the syntax right to add the block that is passed to establishConnectionForURL as a property of the calling class).

Alternatively the httpConnectionFinishedWithError could just call another function, but in that situation I’d have to pass self as a parameter, how would I obtain self within the block in this case?

Are there other more elegant solutions?

Many thanks

EDIT:
Updated to show example of compilation error

void (^httpConnectionFinishedWithError)(NSError*) = 
^(NSError* error) 
{
    self.boolProperty = NO; // here
}
  • 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-04T11:51:41+00:00Added an answer on June 4, 2026 at 11:51 am

    If you’re using ARC, you can define a weak variable referring to self:

    __weak ClassOfSelf *_self = self;

    and use that in the block. If you’re not using ARC I believe you want to use __block instead of weak. In both cases the block will not retain self so you can avoid retain cycles.

    The best way I know of to store/use a block in class is to typedef it first:

    typedef void (^HttpConnectionFinishedWithError)(NSError*);

    Then create an instance variable for the block storage:

    HttpConnectionFinishedWithError httpCompletionBlock;

    In your init you create the block and assign it to the instance variable. Just remember to copy the block first so that it’s moved to the heap (blocks are created on the stack, which means they disappear after the scope they’re created in goes away):

    - (id) init{
      if (self = [super init]){
        __weak id _self = self; //or use class of self instead of id
        httpCompletionBlock = [^(NSError *err){
          .....code here, use _self instead of self
        } copy];
      }
      return self;
    }
    

    (example using ARC)

    Now httpCompletionBlock can be used like normal within the class. No retain cycles are created as long as you use _self instead of self in the block code and you don’t directly reference any instance variables within the block. Remember referencing _iVar is the same as self->_iVar. So the block will capture self. Instead use: _self->_iVar or create a property/get-method to access the instance variable.

    If you’re using ARC, but are targeting iOS 4, you can’t use __weak because it’s not available and you can’t use __block because it won’t prevent the block from retaining the variable under ARC. Instead, you can use __unsafe_unretained. Just remember that __unsafe_unretained does not resolve to nil if the object the variable refers to is dealloc’d…so it’s unsafe. You need to be careful how you use it. For example, if you pass another object this block and self gets dealloc’d. Any reference to _self will cause an error. A way to get around this is to create an inline “intermediate” block that will retain self only so long the block is needed. For example, instead of passing the block directly, do something like this:

    self.httpConnection establishConnectionForURL: url andConnectionFinishedWithErrorBlock:^(NSError* error)
                {
                    httpCompletionBlock(error);
                }];
    

    In the above case, httpcompletionBlock is an instance variable, so self get’s captured by the block in the local scope. You’re assured now that self won’t go away until after httpCompletionBlock is executed. You’ve still avoided a retain cycle because only the inline block is retaining self, not the stored block.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

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.