restart a request

1,710 views
Skip to first unread message

ggould75

unread,
Jul 22, 2010, 5:32:29 AM7/22/10
to ASIHTTPRequest
I would like to restart a connection re-invoking [request
startAsynchronous] if for example the first request does not return
expected results.
The problem is that the second attempt to call startAsynchronous raise
an exception:

*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** -[NSOperationQueue
addOperation:]: operation is finished and cannot be enqueued'

I'm pretty new to ASIHTTPRequest, so maybe I'm trying to do an
unsupported operation...
I'm doing something wrong? Should I create another request and then
call startAsynchronous on it?
Thanks


- (void)viewDidLoad {
[super viewDidLoad];

// remote asynchronous call
NSURL *url = [self getConnectionURL];

mainHTTPRequest = [[ASIHTTPRequest requestWithURL:url] retain];
[mainHTTPRequest setDelegate:self];
[mainHTTPRequest startAsynchronous]; // ***** FIRST CALL *****
}


- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];

BOOL repeat = [self checkIfRepeatNeeded:responseString];

if (repeat) {
[request startAsynchronous]; // ***** SECOND CALL *****
return;
}

// ... further processing...
}

Ben Copsey

unread,
Jul 22, 2010, 7:59:02 AM7/22/10
to asihttp...@googlegroups.com
Hi

> I would like to restart a connection re-invoking [request
> startAsynchronous] if for example the first request does not return
> expected results.
> The problem is that the second attempt to call startAsynchronous raise
> an exception:
>
> *** Terminating app due to uncaught exception
> 'NSInvalidArgumentException', reason: '*** -[NSOperationQueue
> addOperation:]: operation is finished and cannot be enqueued'

Yes, you cannot restart a request like this.

One option would be to make a copy of your request, and start that:

ASIHTTPRequest *newRequest = [[request copy] autorelease];
[newRequest startAsynchronous];

But, if you're just doing a simple GET request, you can just as easily create a new request with the same url.

Best

Ben

Matt Di Pasquale

unread,
Jun 2, 2011, 9:29:43 PM6/2/11
to asihttp...@googlegroups.com
What if I have a model class with an ASIHTTPFormDataRequest *request ivar? Would something like this work?

- (void)dealloc {

    [request clearDelegatesAndCancel];

    [request release];

    [super dealloc];

}


- (void)sendRequest {

    [request clearDelegatesAndCancel];

    [request release];

    NSURL *url = [[NSURL alloc] initWithString:@"https://example.com/"];

    request = [[ASIFormDataRequest alloc] initWithURL:url];

    [url release];

    request.delegate = self;

    [request setPostValue:@"value1" forKey:@"key1"];

    [request setPostValue:@"value2" forKey:@"key2"];

    [request setPostValue:session forKey:@"iVarKey"];

    [request startAsynchronous];

}


The first time i call -sendRequest, request will be nil. That should be fine. Any time after the first that I call it, the first two lines should reset the request correctly, right?


Thanks!


Matt

Matt Di Pasquale

unread,
Jun 2, 2011, 9:41:39 PM6/2/11
to asihttp...@googlegroups.com
Also, does ASIHTTPRequest have an instance method that does:

[request clearDelegatesAndCancel];

[request release];


Or, should I just create an ASIHTTPRequest category and define a method that does the above?


I'm asking because I want to write this code in four spots: dealloc, sendRequest, requestFinished, and requestFailed.


Thanks!


Matt


Matt Di Pasquale

unread,
Jun 2, 2011, 9:52:59 PM6/2/11
to asihttp...@googlegroups.com
Wait, if I call [request release] will that call my delegate and/or queue’s failure delegate methods like [request cancel] does? If not, then can I just do [request release] in sendRequest, requestFinished, and requestFailed; and do both [request clearDelegatesAndCancel] and then [request release] in dealloc?

Joseph Heenan

unread,
Jun 3, 2011, 6:05:19 AM6/3/11
to asihttp...@googlegroups.com
On 03/06/2011 02:52, Matt Di Pasquale wrote:
Wait, if I call [request release] will that call my delegate and/or queue�s failure delegate methods like [request cancel] does? If not, then can I just do [request release] in sendRequest, requestFinished, and requestFailed; and do both [request�clearDelegatesAndCancel] and then [request release] in dealloc?

You must call cancel and clear the delegate before you release your reference to the request. Calling release will not cause the request to immediately stop, so the delegate may be called later on (by which time it might not exist).

I'm on an older version, but I do:

request.delegate = nil; // after this line there are guaranteed to be no more calls to delegate
[request cancel];
[request release];


Cheers,

Joseph


Reply all
Reply to author
Forward
0 new messages