簡體   English   中英

如何檢測NSURLConnection sendAsynchronousRequest是否完成

[英]How to detect if NSURLConnection sendAsynchronousRequest is finished

我正在使用sendAsynchronousRequest:queue:completionHandler:上傳一個文件(我已經刪除了一些舊的第三方庫,轉而使用這種原生方法進行直接調用。我已經按原樣保留了NSURLRequest和字典。)。 我無法弄清楚如何告訴它如何完成推送文件。 我認為在completionHandler中會定期調用,我們可以檢查傳入的參數,但它們似乎不包含我需要的東西。

- (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);
                               }
                           }];

}

根據文檔 ,在異步發送結束時調用一次完成處理程序塊。
如果error為nil,則操作成功(不確定為什么要測試數據長度)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM