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 8208497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:23:25+00:00 2026-06-07T09:23:25+00:00

I have a read-only property isFinished in my interface file: typedef void (^MyFinishedBlock)(BOOL success,

  • 0

I have a read-only property isFinished in my interface file:

typedef void (^MyFinishedBlock)(BOOL success, NSError *e);

@interface TMSyncBase : NSObject {
     BOOL isFinished_;
}

@property (nonatomic, readonly) BOOL isFinished;

and I want to set it to YES in a block at some point later, without creating a retain cycle to self:

- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
    __weak MyClass *weakSelf = self;
    MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
        [weakSelf willChangeValueForKey:@"isFinished"];
        weakSelf -> isFinished_ = YES;
        [weakSelf didChangeValueForKey:@"isFinished"];
        theFinishedBlock(success, e);
    };

    self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}

I’m unsure that this is the right way to do it. Will this code leak, or break, or is it fine? Perhaps there is an easier way I have overlooked?

  • 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-07T09:23:27+00:00Added an answer on June 7, 2026 at 9:23 am

    Passing block variable can be nil, check before calling or add assert on start of the function or you will crash

    Since you are not retaining self and we assume that you execute some long task on background thread by the time your code get’s executed weakSelf can be nil ( hopefully you are using ARC and 5.0 so you have niled weak references ).

    If you don’t have real weak references ( < 5.0, no ARC, compiler would still accept __weak but it wouldn’t matter ) this would lead to crash.

    Also accessing ivar using ‘->’ will lead to crash if object pointer is nil so you need to make sure it doesn’t happen.

    Even if you do the code as dasblinkenlight wrote it can crash if weakSelf will be nil at the moment, let’s say you dispatch the block on background thread and then object get released before block’s executes, this makes weakSelf nil thus accesing it by using ‘->’ will lead to crash. In that case I would modify the code as follows:

    __weak MyClass *weakSelf = self;
    MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
        MyClass *strongSelf = weakSelf;
        //! whatever task you want executed
        strongSelf.isFinished = YES;
        theFinishedBlock(success, e);
    };
    

    Also you could test if weakSelf is nil to prevent expensive task from execution if it doesn’t make sense ( object is already destroyed ). But this depends on use case.

    But there is also other case you need to consider when programming with blocks, for example:
    You can have a job object instance that’s only role is to execute some task in background, in that case this code could fail because you would create new task and it could be deallocated before block executes on background thread, in that case you should retain self and don’t retain block in the object ( this will prevent retain cycle ).

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

Sidebar

Related Questions

The scenario is really simple. I have a read-only collection property of my custom
I have a read-only property on a class that I am testing. public string
If I have a read-only string property, is it necessary to specify strong (or
UIImage has a read-only property CGImage. I have to read its pixels to a
In a Python program I have a read-only property that I create using bla
I have created a read-only property(name) in class1. How can I use this name
Let's say I have a Concrete class with a Read-Only property e.g. public class
In my interface (.h) file, I have @property(readonly) NSString *foo; and in my implementation
I have Class1 with a read-only bindable property called age : public class Class1
If I have a read-only property on an object that fills itself via the

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.