z2k
z2k

Reputation: 10390

How to detect if NSURLConnection sendAsynchronousRequest is finished

I am using sendAsynchronousRequest:queue:completionHandler: upload a file (I have ripped out some old third party library in favor of a direct call with this native method. I have left the NSURLRequest and dictionary as is.). I cannot figure out how to tell how it is finished pushing the file. I would think that in the completionHandler that would be called periodically and that we could inspect the params passed in, but they don't seem to contain what I need.

- (void)sendToServer:(NSString*)url asset:(MYAsset *)asset path:(NSString *)file completion:(MYUploaderBoolBlock)completion{
    NSDictionary *uploadParameters = asset.s3Info;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:uploadParameters[@"my_url"]]];
    request.HTTPMethod = @"PUT";
    request.timeoutInterval = 300;
    [request addValue:uploadParameters[@"date"] forHTTPHeaderField:@"Date"];
    [request addValue:uploadParameters[@"authorization"] forHTTPHeaderField:@"Authorization"];
    [request addValue:uploadParameters[@"content_type"] forHTTPHeaderField:@"Content-Type"];
    [request addValue:asset.md5 forHTTPHeaderField:@"Content-MD5"];
    [request addValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
    [request addValue:[@(asset.sizeInKB) stringValue] forHTTPHeaderField:@"Content-Length"];
    request.HTTPBodyStream = [NSInputStream inputStreamWithFileAtPath:file];

    DDLogInfo(@"Uploading %@ to server", uploadParameters[@"my_url"]);
    DDLogInfo(@"HTTP Headers: %@", [request allHTTPHeaderFields]);

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:self.queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if ([data length] > 0 && error == nil){
                                   // I am expecting that this can be used to detect progress of the file upload
                                   // as well as completion?
                                   NSLog(@"received data %d/%d =%f%", data.length, response.expectedContentLength, data.length/(float)response.expectedContentLength);
                                   if(data.length == response.expectedContentLength){
// This never fires because expectedContentLength is always -1
                                       completion(YES);
                                   }
                               }
                               else if ([data length] == 0 && error == nil){
                                   NSLog(@"reply empty");
                               }
                               else if (error != nil && error.code == -1001){
                                   NSLog(@"timed out");
                               }
                               else if (error != nil){
                                   NSLog(@"Error %@", error.localizedDescription);
                                   completion(NO);
                               }
                           }];

}

Upvotes: 1

Views: 1347

Answers (2)

Gabrail
Gabrail

Reputation: 220

Upvotes: 0

Dan Shelly
Dan Shelly

Reputation: 6011

According to the documentation, the completion handler block is called once at the end of the asynchronous send.
if error is nil, the operation succeeded (not sure why you test the data length).

Upvotes: 3

Related Questions