Search code examples
iosobjective-cautomatic-ref-countingperformselector

ARC: returning allocated object from a method called by performSelector, is it OK?


I'm not sure if i cause a leak here, is it ok to return allocated NSError back to the calling method by perform selector? Is it OK to create the NSMutableArray and store it in the same object i got for the callback? and later pass it to the delegate? The code works fine, but because i'm new to arc i have the fear of doing something wrong.

(i'm using perform selector because my selector is dynamic. just for the example i wrote it statically).

AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request 
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

        //-----------------Callback--------------------

        #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        SEL callback = @selector(getOperationCallback:);
        NSError *error = [self performSelector:callback withObject:operation];

        //------------------Delegate Call---------------
        if(operation.delegate)
            [operation.delegate onFinish:operation.requestIdentifier error:error 
                                                    data:operation.parsedObject];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        //------------------Delegate Call---------------
        if(operation.delegate)
            [operation.delegate onFinish:operation.requestIdentifier error:error data:nil];

    }];

- (NSError *)getOperationCallback:(AFHTTPRequestOperation *)operation{

    NSArray *rawJson = (NSArray *)operation.jsonObject;
    NSError *error;

    NSMutableArray *array = [[NSMutableArray alloc] init];

    for(id json in rawJson){
        MyObject *object = [[MyObject alloc] initWithJson:json];
        if(object){
            [array addObject:object];
        }else{
            error = [NSError errorWithDomain:@"myErrors" code:1000 userInfo:nil];
            break;
        }
    }

    operation.parsedObject = array;

    return error;
}

Solution

  • is it ok to return allocated NSError back to the calling method by perform selector?

    Is it OK to create the NSMutableArray and store it in the same object i got for the callback?

    and later pass it to the delegate?

    Answer to all questions are OK, nothing wrong with anything. All objects are created in autorelease way.