使用AFHTTPRequestOperation下载文件时AFNetworking +奇怪

时间:2013-04-07 16:45:49

标签: download afnetworking

伙计们。我的文件中有这段代码。

AFHTTPClient *_client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://192.168.1.1/file/download/"]];
[_client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[_client setDefaultHeader:@"Content-Type" value:@"application/json;charset=utf-8"];
[_client setDefaultHeader:@"Accept" value:@"*/*"];
[_client setDefaultHeader:@"Content-Length" value:@"858663"];

NSURLRequest *request = [_client requestWithMethod:@"GET" path:@"a.png" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"download %lld/%lld bytes", totalBytesWritten, _task.fileTotalBytes);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //some code       
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //some code
}];

[_client enqueueHTTPRequestOperation:operation];

下面是控制台上的donwload文件日志输出。

23:55:13.731 MyP下载1258/858663字节 23:55:14.566 MyP下载815538/858663字节 23:55:14.567 MyP下载844994/858663字节 23:55:34.352 MyP下载847874/858663字节 23:55:34.354 MyP下载849314/858663字节 23:55:34.362 MyP下载850754/858663字节 23:55:34.364 MyP下载852186/858663字节 23:55:34.366 MyP下载853626/858663字节 23:55:34.372 MyP下载855066/858663字节 23:55:34.374 MyP下载856506/858663字节 23:55:34.375 MyP下载857946/858663字节 23:55:34.375 MyP下载858663/858663字节

为什么最后几个块文件花了20秒?

特殊23:55:14.567 MyP下载844994/858663字节
到23:55:34.352 MyP下载847874/858663字节

似乎线程睡了20秒。

1 个答案:

答案 0 :(得分:0)

我fix.just删除行[_client setDefaultHeader:@“Content-Length”值:@“858663”];