ahwulf
ahwulf

Reputation: 2584

How do I get ARC to retain a delegate long enough to have it called in a block?

In the following code fragment, using ARC, how do I get the delegate to live long enough to call the two method?

Current I get a compiler error

Bad receiver type ' __autoreleasing id * '

I assume I need to do something to make ARC retain the delegate and release it when it done calling but not sure what the right thing to do is.

- (BOOL) requestFromURL:(NSString*)url withDelegate:( id<SimpleDataDelegate>*) delegate
{
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."]]
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if ( error )
         {
             [delegate gotFailure:data];
         }
         else
         {
             [delegate gotResult:data];
         }
     }];

    return YES;
}

Upvotes: 0

Views: 930

Answers (1)

Nate
Nate

Reputation: 31045

I think your method signature should probably be

- (BOOL) requestFromURL:(NSString*)url withDelegate:(id<SimpleDataDelegate>) delegate

instead of

- (BOOL) requestFromURL:(NSString*)url withDelegate:(id<SimpleDataDelegate>*) delegate

Notice the lack of a * in the first one, in the second parameter. Try that, and see if the error goes away. Report back if not.

Upvotes: 7

Related Questions