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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:47:07+00:00 2026-06-15T23:47:07+00:00

I have two instances of different classes who both need to add a completion

  • 0

I have two instances of different classes who both need to add a completion block to a particular operation. I’ll try to explain the problem generically rather than having to explain everything my app is attempting to do.

A view controller is calling into an instance of a resource manager class for it to save a resource. The resource manager then calls into the class of the resource to be saved to get a network operation for the save.

The instance of the resource creates the operation and gives it a completion block that will affect the state of the resource when it fires.

This is where my problem is – the resource class also needs to add a completion block to this operation in order for the view controller to be informed of the success or failure of the save.

Here’s a snippit of the save method on the manager:

-(void)save:resource withCompletion:completion
{
.
.
.

NSOperation *operation = [resource operationForSave];

NSOperation __weak *weakOperation = operation;
void(^__weak resourceCompletion)(void)= operation.completionBlock;

[operation setCompletionBlock:^{
    if (resourceCompletion) {
        resourceCompletion();
        }

    if (completion) {
       if (weakOperation.error) {
            completion(NO, operation.error);
            }
        else {
            completion(YES, nil);
            }
        }
    }];

.
.
.
// add the operation to a network operation queue
}

While I think this will technically work, I’m not crazy about it. It feels pretty funky. I would prefer to have one block encapsulating the second block, but this isn’t possible because the view controller and the resource are creating their own completion blocks, and the manager class is the one that has to smash them together.

Is there a more elegant way to chain these two completion blocks together in this situation, or is my current method of creating a block to contain the original two blocks the best I’m going to get?

Any input would be great appreciated.

  • 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-15T23:47:08+00:00Added an answer on June 15, 2026 at 11:47 pm

    The code you posted will probably not work. When you replace the operation’s completion block with your own block, you’re probably removing the only strong reference to the original completion block (set by the resource). So your resourceCompletion variable, being weak, will become nil by the time setCompletionBlock: returns.

    Just making resourceCompletion strong should fix the problem. But if you want to do it in a cleaner way, modify the operationForSave message (on the resource) to take a completion block itself:

    __block NSNetworkOperation *operation = [resource operationForSaveWithCompletion:^{
        NSError *error = operation.error;
        completion(error == nil, error);
    
        // Break the retain cycle between this block and the operation object.
        operation = nil;
    }];
    

    And make it the job of the resource’s own internal completion block to call the completion block you provide.

    If you don’t want to or can’t modify the resource’s API, you can still simplify your code by eliminating the weak references:

    __block NSNetworkOperation *operation = [resource operationForSave];
    __block void (^priorCompletion)(void) = operation.completionBlock;
    operation.completionBlock = ^{
        if (priorCompletion) {
            priorCompletion);
            // Break possible retain cycle.
            priorCompletion = nil;
        }
    
        NSError *error = operation.error;
        completion(error == nil, error);
        // Break the retain cycle between this block and the operation object.
        operation = nil;
    };
    

    Also, I sincerely hope you don’t really have a class named NSNetworkOperation, because Apple reserves the NS prefix (and all other two-letter prefixes) for its own use.

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

Sidebar

Related Questions

I have two classes that extend the same abstract class. They both need the
I have two different web applications running on two different instances of tomcat. I
I need to override styles for two EditText instances. I have two issues regarding
I have two Python classes written in two different files. One is written in
I have created Guice binding annotations that allow me to bind two different instances
I have two instances of a model and find that they seem to share
I have two instances of VS2010 running on same machine.One VS instance has a
I have two instances of an Address.ascx control in an ASP.NET MVC page. <h1>Shipping
I have two instances of CFMutableBitVector , like so: CFBitVectorRef ref1, ref2; How can
I have two Java instances of java.util.Date and I have to find out if

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.