我想做以下事情: 我正在创建一个ASIFormDataRequest,我想在完成后调用一个新的:
所以我的代码看起来像这样:
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];
}
我不知道我必须在requestFinished中放置什么才能确定哪两个请求已经完成?或者如果还有其他方法可以这样做?
答案 0 :(得分:2)
ASIFormDataRequest扩展了ASIHTTPRequest类,它的“tag”属性为NSInteger类型。
您可以为您的请求设置不同的代码,当您收到回复时,您可以检查该请求的代码是什么。
这使你能够确定它是哪一个。
答案 1 :(得分:2)
您可以在完成不同的请求时调用不同的方法。
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.
}
答案 2 :(得分:1)
由于您的请求是按顺序执行的,因此维护一个整数实例变量/属性就足够了,该变量/属性在您启动第一个请求时设置为零,并在requestFinished
中递增。
已添加:您的代码中也存在错误:在requestFinished
中,您应该使用[request_2 startAsynchronous];
启动新请求,而不是重新启动上一个请求。< / p>
答案 3 :(得分:1)
如果seconde请求不使用第一个请求的结果,您可以将所有请求放在队列中:
[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];
除了Martin R responece之外,您还可以使用[[self networkQueue] requestsCount]获取requestInQeuFinished:
中 REST 的请求数。