How can I know when my ASIHttpFormDataRequest ends ? Actually I am doing two of them in a row. The first one consist in sending a message while the other one consist in retrieving all the messages from a database.
Or it happens sometimes that my first message has not been sent yet and my second request just give me the unuploaded list of messages.
I would like to delay the second one at the en开发者_如何转开发d of the first one. Thanks !
Any ideas ?
Thank you :)
It is a good practice to use requests asynchronously.
To find out if a request has finished or not, you could do this by simply conforming your class to the [ASIHTTPRequestDelegate][1]
.
It gives you a bunch of methods to implement for a successful, failed or a redirected request.
In the given case you're looking for
- (void)requestFinished:(ASIHTTPRequest *)request;
You would have to set the delegate of the request to the current calling class.
Depending on the result of your current request you may then fire another request. You could put an if-statement
in there to check the URL and the response code and fire your next request accordingly. (There are many other ways to determine when you could call the next request. But the easiest would be to listen to the result of the first request.)
Here's a bit of code from the ASIHTTPRequest Projects Documentation:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
HTH
精彩评论