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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:28:39+00:00 2026-06-13T06:28:39+00:00

How do I refactor arbitrary blocks? I have two functions with vastly different signatures

  • 0

How do I refactor arbitrary blocks?

I have two functions with vastly different signatures that I would like to pass as a block so that I can ensure thread-safety, since setenv() and unsetenv() are not guaranteed to be thread-safe:

Old Code

-(X*)foo1
{
  X* x;
  @synchronized( self )
  {
    setenv( ... );
    x = worker_1( ... );
    unsetenv( ... );
  }
  return x;
}

-(Y*)foo2
{
  Y* y;
  @synchronized( self )
  {
    setenv( ... );
    y = worker_2( ... );
    unsetenv( ... );
  }
  return y;
}

Note that the two chunks of code are identical except that the signatures to worker_1() and worker_2() are vastly different.

In other words, I need to refactor the same wrappers, but completely different guts returning arbitrary objects.

New Code

- (void)aThreadSafeWrapper:my_block  // broken: needs fixing
{
    @synchronized( self )
    {
       setenv( ... );
       my_block();                   // broken: needs fixing
       unsetenv( ... );
    }
}

Is there any way I can accomplish this refactoring in Objective-C?

  • 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-13T06:28:40+00:00Added an answer on June 13, 2026 at 6:28 am

    I don’t know if I understand your question, but it looks like all you need is something like…

    - (void)performWithMySpecialEnvironment:(void(^)(void))block
    {
        @synchronized( self )
        {
           setenv( ... );
           block();
           unsetenv( ... );
        }
    }
    

    Then, you can do whatever you want in your block…

    [foo performWithMySpecialEnvironment:^{
        X x = worker_1( ... );
    }];
    
    [foo performWithMySpecialEnvironment:^{
        Y y = worker_2( ... );
    }];
    

    Note, that I do not endorse the @synchronized/setenv/unsetenv usage, but since that’s what you are doing, and it’s not part of the real question at hand, I’ll leave it alone since it may just confuse the real “blocks” issue.

    EDIT

    +1 for the wrapper suggestion. Actually, if I change the block signature to return (void*) and do some really ugly casting, I can get
    this to work. If this ends up obfuscating, I may just have to
    copy-paste the wrapping code (yuck). I love ObjC, but this is where
    some functionality like C++ templates would come in handy. I will
    accept this answer if no better solution comes along. tyvm! – kfmfe04

    Apparently, you need the casting and return because you want to access the variable outside the block. That’s easily remedied.

    __block X x;
    [foo performWithMySpecialEnvironment:^{
        x = worker_1( ... );
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I refactor this so that numberOfItems doesn't have to be declared as
How would you refactor this, keeping in mind that you have dozens more of
It's Friday refactor time!!! I have an object that I use to generate SQL
In order to refactor my MATLAB code, I thought I'd pass around functions as
How can I refactor(move) a Java method in classA into classB and have all
I am trying to refactor a class that I have into 2 classes. Unfortunately
I have a set of 2 or more objects that I'd like to order.
I would like to refactor a large legacy application originally written in Visual Basic
How can refactor these methods so that they don't rely on using an instance
The refactor tool I'm using often suggests that I change something like this: string

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.