AFNetworking 取消 post 请求

AFNetworking cancel post request

我知道,这个问题以前问过。但在这种情况下对我不起作用。我正在为传输层开发 AFNetworking。我的代码如下。

AFHTTPRequestOperation *post; AFHTTPRequestOperationManager *经理;


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);
    }];

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

[post 取消];

错误: [AFHTTPRequestOperation cancel]:消息发送到释放实例 0x1723cbf0

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

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

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

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

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

当你调用上面的方法时,所有的请求都会失败,failureblock 会被调用。所以你需要检查失败块中的操作是否为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];