Question

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;
}
Was it helpful?

Solution 2

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.

OTHER TIPS

Generally, performSelector: in ARC could only cause a leak, if the selector you pass to it starts with alloc, new, retain, copy, or mutableCopy.

As long as the return value of the method your are invoking via performSelector:withObject: is an object, it's perfectly ok to do that.

It won't leak since ARC will take care of releasing array and error is autoreleased.

In general, steer clear of performSelector:. The reason being that ARC cannot help you. Even if you think your app works and you've tested that it does, it might break later down the line when you change something.

Sure, if the selector you're calling does not start with alloc, new, copy, mutableCopy, etc then it won't be a problem. But there's cases, such as using __attribute__((ns_returns_retained)) that make it non-obvious that a method might return something retained. In any case, having code that ARC cannot help you out with, is a bad thing.

There's always a way to make it such that you don't have to use performSelector:. Why not make use of a callback block for example?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top