views:

102

answers:

1

I have a instance of ASINetworkQueue, and I added instances of ASIHTTPRequest to the queue; meanwhile I have delegate methods set for the Queue as well as each requests:

[submittingReportQueue setDelegate:self];
[submittingReportQueue setRequestDidFailSelector:@selector(submitReportQueueWentWrong:)];
[submittingReportQueue setQueueDidFinishSelector:@selector(submitReportQueueFinished:)];

and in a loop I added request to the Queue, add call [submittingReportQueue go] outside of the loop.

ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
 NSString *auth = [[AuthenticationManager sharedInstance] authenticationHeaderValue];
 request addRequestHeader:@"Authorization" value:auth];
 [request addRequestHeader:@"Content-Type" value:@"application/json"];
 NSString *jsonString =[self jsonStringForExpenseReport:report];
 [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
 [request setDelegate:self];
 [request setUserInfo:[NSDictionary dictionaryWithObject:report forKey:@"Report"]];
 [request setDidFailSelector:@selector(submitReportRequestWentWrong:)];
 [request setDidReceiveDataSelector:@selector(submitReportRequestDone:)];
 [requests addObject:request];
 [submittingReportQueue addOperation:request];

Following are my delegate methods:

- (void)submitReportQueueWentWrong:(ASINetworkQueue *)queue
{
    NSLog(@"Submit Report WentWRong");

//
- (void)submitReportQueueFinished:(ASINetworkQueue *)queue
{
    NSLog(@"Submit Report QueueFinished");

}
//
- (void)submitReportRequestWentWrong:(ASIHTTPRequest *)request
{
        NSLog(@"Submit Report Queue went wrong");
}

//
- (void)submitReportRequestDone:(ASIHTTPRequest *)request

{//do work here}

However ASIHTTPRequest.m throws exception in following block of code:

// Does the delegate want to handle the data manually?
if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
  NSMethodSignature *signature = [[[self delegate] class]     
           instanceMethodSignatureForSelector:[self didReceiveDataSelector]];
  NSInvocation *invocation = [[NSInvocation invocationWithMethodSignature:signature] 
           retain];
 [invocation setSelector:[self didReceiveDataSelector]];
 [invocation setArgument:&self atIndex:2];
 NSData *data = [NSData dataWithBytes:buffer length:bytesRead];
 [invocation setArgument:&data atIndex:3];
 [invocation retainArguments];
 [self performSelectorOnMainThread:@selector(invocateDelegate:) withObject:invocation waitUntilDone:[NSThread isMainThread]];

[invocation setArgument:&data atIndex:3]; throws exception, Error message is 'NSInvalidArgumentException', reason: '* -[NSInvocation setArgument:atIndex:]: index (3) out of bounds [-1, 2]'

What did I do wrong?

Thanks `

A: 

The problem is here:

[request setDidReceiveDataSelector:@selector(submitReportRequestDone:)];

and here:

- (void)submitReportRequestDone:(ASIHTTPRequest *)request


The didReceiveDataSelector is probably not the one you wanted, as it's called each time a chunk of data is received - I suspect you want to be called when the request has finished, so you should instead set the requestDidFinish selector:

[request setRequestDidFinishSelector:@selector(submitReportRequestDone:)];


For your info, the error you're seeing essentially means "The selector you have given me does not have the right call signature" or more specifically "I'm trying to call a method that takes 2 parameters, and your method only takes 1". (If I remember correctly, the reason why it fails on index 3 is that the first parameter is a hidden parameter containing the object.)

JosephH