0

我想做以下事情:我正在制作一个 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 中放置什么以便确定两个请求中的哪一个已经完成?或者是否有其他方法可以这样做?

4

4 回答 4

2

ASIFormDataRequest 扩展了 ASIHTTPRequest 类,它具有“标签”属性作为 NSInteger 类型。

您可以为您的请求设置不同的标签,当您收到响应时,您可以检查该请求的标签是什么。

这使您可以确定它是哪一个。

于 2012-09-16T10:06:44.067 回答
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.
}
于 2012-09-16T10:26:58.217 回答
1

由于您的请求是按顺序执行的,因此只需维护一个整数实例变量/属性,当您启动第一个请求时将其设置为零,并在requestFinished.

补充:您的代码中还有一个错误:requestFinished您应该使用 启动新请求[request_2 startAsynchronous];,而不是重新启动以前的请求。

于 2012-09-16T09:53:49.210 回答
1

如果第二个请求不使用第一个的结果,您可以将所有请求放在一个队列中:

[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响应之外,您还可以使用 [[self networkQueue] requestsCount] 来获取REST中的请求数requestInQeuFinished:

于 2012-09-16T10:00:37.223 回答