如何使用相同的参数重试AFNetworking连接

时间:2015-10-29 14:43:10

标签: objective-c afnetworking

我有以下代码:

NSDictionary *parameters = @{@"parameter1":objectVariable1,@"parameter2":objectVariable2};
[MANAGER POST:@"http:www.myserver.com/somelink" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     //PROCESS RESPONSE
 }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     //PROCESS FAILURE
 }];

如果失败,我如何考虑objectVariable1& amp; objectVariable2可以随时更改(当连接发送到服务器时),因此必须发送与之前相同的post参数。
可以从错误分支中的AFHTTPRequestOperation *operation获取参数吗?

可以肯定的是,例如,情况可能如此:
- objectVariable1 = 1
- 发送与objectVariable1 = 1的连接 - objectVariable1 = 2
- 连接失败,应该使用objectVariable1 = 1

重试

1 个答案:

答案 0 :(得分:0)

快速而肮脏的解决方案:

-(void) post: (NSString *) post withParams: (NSDictionary *) params andReplies: (NSInteger) replies
{
    [MANAGER POST:post parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //PROCESS RESPONSE
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         //PROCESS FAILURE
        if (replies > 0) {
            [self post: post withParams: params andReplies: replies - 1];
        }
     }];
}