ghostrider
ghostrider

Reputation: 5259

making another http request when the first one is done iOS

I want to do the following: I am making an ASIFormDataRequest and I want when it is finished, invoke a new one:

So my code will look something like this:

 ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setPostValue:mail forKey:@"email"];
    [request setPostValue:cod forKey:@"code"];
    [request setPostValue:tab forKey:@"table"];
    [request setPostValue:name forKey:@"name"];
    [request startAsynchronous];

- (void)requestFinished:(ASIHTTPRequest *)request
{
   //request 1 is done, invoke request 2
 ASIFormDataRequest *request_2=[ASIFormDataRequest requestWithURL:url];
    [request_2 setDelegate:self];
    [request_2 setPostValue:other_data forKey:@"info"];
    [request_2 setPostValue:phone forKey:@"phone"];
    [request startAsynchronous];
}

and I do not know what I must place in requestFinished so as to identify which of the 2 requests has finished? or if there is another way for doing so?

Upvotes: 0

Views: 394

Answers (4)

Martol1ni
Martol1ni

Reputation: 4702

You could just invoke different methods when the different requests are finished.

ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(firstRequestFinished:)];

- (void)firstRequestFinished:(ASIHTTPRequest *)request
{
   //request 1 is done, invoke request 2
    ASIFormDataRequest *request_2=[ASIFormDataRequest requestWithURL:url];
    [request_2 setDelegate:self];
    // Work with your request, and then
    [request setDidFinishSelector:@selector(secondRequestFinished:)];
    [request startAsynchronous];
}

- (void)secondRequestFinished:(ASIHTTPRequest *)request
{
   //request 2 is done, invoke request 3
    ASIFormDataRequest *request_3=[ASIFormDataRequest requestWithURL:url];
    [request_3 setDelegate:self];
    // Work with your request, and then
    [request setDidFinishSelector:@selector(thirdRequestFinished:)];
    [request startAsynchronous];
}

- (void)thirdRequestFinished:(ASIHTTPRequest *)request
{
   // Do whatever should be done now.
}

Upvotes: 2

Martin R
Martin R

Reputation: 540065

Since your requests are executed sequentially, it would suffice to maintain an integer instance variable/property which is set to zero when you start the first request, and incremented in requestFinished.

ADDED: There is also an error in your code: In requestFinished you should start the new request with [request_2 startAsynchronous];, instead of re-starting the previous request.

Upvotes: 1

user1111278
user1111278

Reputation: 126

ASIFormDataRequest extends ASIHTTPRequest class and it has "tag" property as NSInteger type.

You can set different tags your requests and when you recieved a response you can check what is the tag of that request.

That makes you identify which one it is.

Upvotes: 2

Red Mak
Red Mak

Reputation: 676

If the seconde request dont use the result of the first one, you can put all requests on a queue:

[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestInQeuFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

for (NSString *tmpDic in array) 
{
    ASIHTTPRequest *link= [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"link"]];
    [[self networkQueue] addOperation:request];
}

[[self networkQueue] go];

in addition to Martin R responece, you can use [[self networkQueue] requestsCount] to get the REST of requests number in requestInQeuFinished:.

Upvotes: 1

Related Questions