MKNetworkOperation的addCompletionHandler消息请求两次问题

刚刚在IOS上面使用访问网络的框架MKNetworkKit,在运用MKNetworkOperation中的addCompletionHandler消息访问网络的时候会出现同一个URL地址访问两次的现象。

出错的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
MKNetworkOperation *operation = [self operationWithPath:path params:params httpMethod:@"GET"];

[operation addCompletionHandler:^(MKNetworkOperation *completedOperation) {
id dict = [completedOperation responseJSON];

if ([completedOperation isCachedResponse]) {

NSLog(@"CompletedOperation cached");

}else {

NSLog(@"CompletedOperation fresh");

}

successBlock(dict);

} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {

DLog(@"%@", [error localizedDescription]);

errorBlock(error);

}];

修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
MKNetworkOperation *operation = [self operationWithPath:path params:params httpMethod:@"POST"];
[operation addCompletionHandler:^(MKNetworkOperation *completedOperation) {

id dict = [completedOperation responseJSON];

if ([completedOperation isCachedResponse]) {

NSLog(@"CompletedOperation cached");

}else {

NSLog(@"CompletedOperation fresh");

}

successBlock(dict);

} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {

DLog(@"%@", [error localizedDescription]);

errorBlock(error);

}];

其实就是修改了请求URL的method,把GET方式修改成POST。

目前本人自测成功,具体是什么原因导致的暂时没有去深究(PS:有可能访问之后在本地具有缓存的内容,所以第一次是从缓存中读取数据,而并没有真正的去访问服务器的内容)

文章目录
,