AFNetworking取消发布请求

时间:2016-01-22 14:45:11

标签: afnetworking-2 afhttprequestoperation request-cancelling

我知道,这个问题之前问过。但这种情况并不适合我。我正致力于AFNetworking的传输层。我的代码如下。

AFHTTPRequestOperation * post; AFHTTPRequestOperationManager * manager;

AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] init] ;

    delegate = (id<ConnectionUtilDelegate>) delegateObject;

    for (Parameter *param in parameterArray)
    {
        [paramDict setObject:param.value forKey:param.name];
    }
    NSDictionary *params = paramDict;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager.operationQueue cancelAllOperations];
    post = [manager POST:[NSString stringWithFormat:@"%@",url] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        [delegate requestCompleted:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [delegate requestFailed:error];
        NSLog(@"Error: %@", error);
    }];

这是我的帖子请求。它的工作就像魅力一样。但有时一些要求可能会迟到。例如,我发送2个不同的帖子请求,名称为P1(Post 1)和P2(Post 2)。如果这些请求响应返回为R1(响应1)而R2(响应2)对我来说是可以的。但是,如果此请求响应作为R2(响应2)而不是R1(响应1)返回,则它是我的应用程序的原因。所以我想在发送新请求时取消旧请求。和

[取消后];

错误: [AFHTTPRequestOperation cancel]:发送到解除分配的实例0x1723cbf0的消息

不适合我。你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

正如我在您的代码中看到的那样,您为每个请求创建一个新的AFHTTPRequestOperationManager实例,然后对新创建的cancelAllOperations的operationQueue执行AFHTTPRequestOperationManager。我建议你创建一个类型为AFHTTPRequestOperationManager的sharedManager,并为你的所有请求使用这个singleton对象:

+ (instancetype)sharedManager {
    static AFHTTPRequestOperationManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [AFHTTPRequestOperationManager manager];
    });
    return instance;
}

然后,您可以在执行新请求之前取消所有请求:

+ (void)cancelAllRequests {
    [[AFHTTPRequestOperationManager sharedManager].operationQueue cancelAllOperations];
}

当您调用上述方法时,所有请求都将失败,并且将调用failure块。因此,您需要检查故障块中的操作是否为cancelled。最后的代码来了:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager sharedManager];
NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] init] ;


delegate = (id<ConnectionUtilDelegate>) delegateObject;

for (Parameter *param in parameterArray)
{
    [paramDict setObject:param.value forKey:param.name];
}
NSDictionary *params = paramDict;

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

AFHTTPRequestOperation *post = [manager POST:[NSString stringWithFormat:@"%@",url] parameters:params

success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [delegate requestCompleted:responseObject];

}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (!operation.cancelled) {
        [delegate requestFailed:error];
    }
}];

[post start];