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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:58:49+00:00 2026-06-14T07:58:49+00:00

I have an NSOperation custom class in which I added a method to accept

  • 0

I have an NSOperation custom class in which I added a method to accept block for easiness such that I could assign the completion block. But, even if I dont call the block, it is being returned to the main calling class and sends message with itself being passed.

Interface :

@class  SKSimpleDownloadOperation;
typedef void(^CompletionBlock)(id Json, NSError *error);
@protocol SKSimpleDownloadDelegate <NSObject>
-(void)operation:(SKSimpleDownloadOperation*)operation didCompleteWithData:(NSData*)data;
-(void)operation:(SKSimpleDownloadOperation*)operation didFailWithError:(NSError*)error;
@end
@interface SKSimpleDownloadOperation : NSOperation
@property(nonatomic, assign) NSInteger statusCode;
-(id)initWithUrlRequest:(NSURLRequest*)request andDelegate:(id<SKSimpleDownloadDelegate>)aDelegate;
-(id)initWithUrlRequest:(NSURLRequest*)request andDelegate:(id<SKSimpleDownloadDelegate>)aDelegate withCompletionBlock:(void(^)(id json, NSError *error))completionBlock;
@end

Implementation:

@interface SKSimpleDownloadOperation()<NSURLConnectionDataDelegate>
@property(nonatomic, strong) NSURLRequest *request;
@property(nonatomic, strong) NSMutableData *data;
@property(nonatomic, assign) id<SKSimpleDownloadDelegate>delegate;
@property(nonatomic, copy) CompletionBlock completionBlock;
@end
@implementation SKSimpleDownloadOperation
@synthesize delegate;
@synthesize request;
@synthesize statusCode;
@synthesize completionBlock;

-(id)initWithUrlRequest:(NSURLRequest *)aRequest andDelegate:(id<SKSimpleDownloadDelegate>)aDelegate withCompletionBlock:(void (^)(id, NSError *))aCompletionBlock{
    if(!(self = [super init])) return nil;
    [self setRequest:aRequest];
    [self setDelegate:aDelegate];
    [self setCompletionBlock:aCompletionBlock];
    return self;
}
-(id)initWithUrlRequest:(NSURLRequest *)aRequest andDelegate:(id<SKSimpleDownloadDelegate>)aDelegate{
    return [self initWithUrlRequest:aRequest andDelegate:aDelegate withCompletionBlock:nil];
}

-(void)main{
    [NSURLConnection connectionWithRequest:[self request] delegate:self];
    CFRunLoopRun();
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
    [self setStatusCode:[response statusCode]];
    [self setData:[NSMutableData data]];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)newData{
    [[self data] appendData:newData];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if(self.delegate && [self.delegate respondsToSelector:@selector(operation:didCompleteWithData:)])
    [[self delegate] operation:self didCompleteWithData:[self data]];
    CFRunLoopStop(CFRunLoopGetCurrent());
//    if(completionBlock != nil){
//        [self performSelector:@selector(callBlockWithNecessaryParameter) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
//    }
}

-(void)callBlockWithNecessaryParameter{
    NSError *error = nil;
    id object = [NSJSONSerialization JSONObjectWithData:[self data] options:NSJSONReadingAllowFragments error:&error];
    // completionBlock(object,error);
    //completionBlock = nil;

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [[self delegate] operation:self didFailWithError:error];
    CFRunLoopStop(CFRunLoopGetCurrent());
}
@end

How I call the operation;

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2248907%22&format=json"]];
    ViewController __weak *__viewController = self;
    NSOperation *operation = [[SKSimpleDownloadOperation alloc] initWithUrlRequest:request andDelegate:nil withCompletionBlock:^(id json, NSError *error) {
        [__viewController populateReceivedJSON:json];
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    operation = nil;
    queue = nil;

-(void)populateReceivedJSON:(id)json{
    NSLog(@"JSON --> %@", json);
}

As you see in the above code I uncommented the call to block in connectionDidFinishLoading: method but the block is being called and the method populateReceivedJSON is being triggered.

And more interesting to me is log shows the same instance of NSOperation subclass as;
JSON --> <SKSimpleDownloadOperation: 0x7462be0>

I could not understand what is the problem here ? When I call delegate it works fine but the block gets called always. I would like to thankyou in advance for your warm suggestions and ideas.

  • 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-14T07:58:50+00:00Added an answer on June 14, 2026 at 7:58 am

    NSOperation Class Reference

    completionBlock
    Returns the block to execute when the operation’s main task is complete.

    -(void (^)(void))completionBlock
    Return Value
    The block to execute after the operation’s main task is completed. This block takes no
    parameters and has no return value.

    Discussion The completion block you provide is executed when the value returned by the isFinished method changes to YES. Thus, this
    block is executed by the operation object after the operation’s
    primary task is finished or cancelled
    .

    If you place the Operation on a Queue the completion block will be call when the operation is completed or cancelled.
    Also your “custom” completion block may be interfering with the “completionBlock” of NSOperation.
    I would suggest using a different name for your custom completionBlock, since it’s not the behaviour of the NSOperation completionBlock that you want, but something else.

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

Sidebar

Related Questions

I have a parser class that is subclass of NSOperation. It is used to
While learning about NSOperation, I wondered why a completion block would have any advantage
I have an NSOperation derived class which performs asynchronous download. Because the download operation
I have an nsoperation class where I want to store that running operation into
I have a custom NSOperation object that is instantiated from a UITableViewController subclass (MusicTableVC).
I have an NSOperation subclass which uses Core Data. It has a custom init
Using iOS 4.3 on an ipad app I have a custom NSOperation that uses
I have a class that's a subclass of NSOperation (actually a subclass of ASIHTTPRequest,
I need your help. I have write my own custom NSOperation class called GetNewsOperation.
I really need help here. I'm desperate at this point. I have NSOperation that

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.