我创建了AFHTTPClient
的子类。
可行,但progress
阻止仅返回最终值(1.0
)。我应该更改以获取setDownloadProgressBlock
的所有计算值?
- (void)exploreVenuesCenter:(CLLocationCoordinate2D)coordinate inRadius:(int)radiusMeters success:(void (^)(NSArray *venues))success failure:(void (^)(NSError *error))failure progress:(void (^)(float progress))progress{
NSDictionary *parameters = @{@"limit": @"10",
@"client_id":kFoursquareClientID,
@"client_secret":kFoursquareClientSecret,
@"radius":@(radiusMeters).stringValue,
@"ll":[NSString stringWithFormat:@"%g,%g",coordinate.latitude, coordinate.longitude],
@"v":kFoursquareVersion};
NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:@"venues/explore" parameters:parameters];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *items = responseObject[@"response"][@"groups"][0][@"items"];
if (success)
{
success(items);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) {
failure(error);
NSLog(error.localizedDescription);
} }];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
//
float progressNo = ((float)totalBytesRead) / totalBytesExpectedToRead;
if (progress) {
progress(progressNo);
}
}];
[self enqueueHTTPRequestOperation:operation];
}