首先,我很抱歉任何拼写或语法错误,因为英语不是我的第一语言。
我正在尝试使用AFNetworking 2.0.2来接收JSON响应,其中包含一个Base64编码的sqlite数据库。问题是在下载过程中不会更新进度视图。一旦完全下载响应,就会立即多次调用下载进度块!
这是我正在使用的代码:
NSURL *url = [NSURL URLWithString:[[Config sharedInstance] databaseDownloadPath]
relativeToURL:[NSURL URLWithString:[[Config sharedInstance] baseServerAdress]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:600];
AFHTTPRequestSerializer * serializer = [AFHTTPRequestSerializer serializer];
[serializer setValue:[[Config sharedInstance] currentCompany] forHTTPHeaderField:@"CurrentCompany"];
[serializer setValue:[[Config sharedInstance] currentUser] forHTTPHeaderField:@"CurrentUser"];
[serializer setValue:[[Config sharedInstance] authorizationToken] forHTTPHeaderField:@"AuthorizationToken"];
[serializer setValue:[[Config sharedInstance] accessGroup] forHTTPHeaderField:@"AccessGroup"];
[serializer setValue:[[Config sharedInstance] applicationId] forHTTPHeaderField:@"Application"];
NSURLRequest * serializedRequest = [serializer requestBySerializingRequest:request
withParameters:@{
@"storeId":[[Config sharedInstance] storeId],
@"compress":@"true"
}
error:nil];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:serializedRequest];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float percent = ((float)totalBytesRead / (float)totalBytesExpectedToRead)*100;
dispatch_async(dispatch_get_main_queue(), ^{
NSlog(@"%f",round(percent));
});
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *_operation, id responseObject) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"%@",[[_operation response] allHeaderFields]);
} failure:^(AFHTTPRequestOperation *_operation, NSError *error) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"ERR: %@", [error description]);
}];
[operation start];
操作开始后,一分钟或两分钟都没有发生任何事情,而收到响应,然后,突然,我在调用成功块之前立即获得了几十个日志。
我还尝试使用以下代码而不是手动设置下载进度块,但得到了相同的结果:
[progressView setProgressWithDownloadProgressOfOperation:operation animated:YES];
我已经检查过并且响应中的Content-Length标头是正确的,所以这不是标题错误。
有没有人遇到过这种情况?