update
I think the problem is with the networkQueue. When I replace [self.networkQueue addOperation:request] with [request startAsynchronous] or [request startSynchronous], it works.
I update the code to make it more clear.
original
I use ASIHTTPRequest to upload a file of json with encoded image (or [request setFile:imagefile...]), but I cannot update the progress.
I got only one output: value: 1.000000, which means the uploads finished.
incrementUploadSizeBy was never triggered.
I have searched online a lot but still cannot find a answer. Here is my code.
+ (ASIEngine *)sharedInstance {
static ASIEngine *sharedInstance = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)init {
if (self = [super init]) {
_networkQueue = [ASINetworkQueue queue];
[_networkQueue setMaxConcurrentOperationCount:MAX_CONCURRENT_OPERATION_COUNT];
[_networkQueue setDelegate:self];
[_networkQueue go];
}
return self;
}
- (void)upload:(NSString *)imageJsonString onCompletion:(void(^)(NSString *responseString))onCompletion onFailed:(void(^)(NSError *error))onFailed {
__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPLOAD_URL]];
[request setShowAccurateProgress:YES];
[request setUploadProgressDelegate:self];
[request setPostValue:imageJsonString forKey:@"imageString"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
onCompletion(responseString);
}];
[request setFailedBlock:^{
onFailed([request error]);
}];
[self.networkQueue addOperation:request];
}
#pragma mark - ASIProgressDelegate
- (void)setProgress:(float)newProgress {
NSLog(@"value: %f", newProgress);
}
- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength {
NSLog(@"data length: %lld", newLength);
}