0

API呼び出しの進行状況バーを作成し、成功して終了したいのですが、AFNetworking 3.0バージョンを使用しています。

進行状況を測定するために次のコードを実行します。

NSURLSessionDataTask *obj =  [manager POST:UrlForGetAllCalEntry parameters:jsonDict progress:^(NSProgress * _Nonnull uploadProgress) {
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

                        if ([[responseObject valueForKey:@"code"] integerValue] == 200)
                        {

                         }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    [TRAVALARMMANAGER setMessage:error.localizedDescription withView:[APPDELEGATE window] textColor:txtMsgColor bgColor:bgMsgColor];
                    NSLog(@"Error: %@", error);
                }];


[manager setDataTaskDidReceiveDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) {
                    if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)
                        return;

                    if (dataTask != obj)
                        return;

                    NSUInteger code = [(NSHTTPURLResponse *)dataTask.response statusCode];

                    if (!(code> 199 && code < 400))
                        return;

                    long long  bytesReceived = [dataTask countOfBytesReceived];
                    long long  bytesTotal = [dataTask countOfBytesExpectedToReceive];

                    NSLog(@"... %lld/%lld",
                          bytesReceived,
                          bytesTotal);
                }];

しかし、メソッドはから戻ります

if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown) 戻ります。

このステートメントは常に true を返します。なぜだか分からない?. ヘッダーも印刷します。「連絡先の長さ」オプションがあります。

4

3 に答える 3

0

うーん、私はこの方法を使用してダウンロードの進行状況を監視していますが、問題なく動作します。

- (void)setDownloadTaskDidWriteDataBlock:(void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite))block {
    self.downloadTaskDidWriteData = block;
}

↓↓↓↓↓↓例↓↓↓↓↓↓

ダウンロード開始:

NSURL *downloadURL = [NSURL URLWithString:@"example.com/file.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];

self.downloadTask = [self.manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    // this progress param is "downloadTask operation" progress, it's not the data receiving progress         

    return nil;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

}];
[self.downloadTask resume];

ダウンロードの進行状況を計算します。

__weak typeof(self) vc = self; 
[self.manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {

    dispatch_async(dispatch_get_main_queue(), ^{
        // here is what you want
        vc.progressView.progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;
    });

}];

結果:

TestProject Image お役に立て ば幸いです。

于 2016-01-21T14:28:05.300 に答える
0

Apple ドキュメントから

解説 この値は、サーバーから受信した Content-Length ヘッダーに基づいて決定されます。そのヘッダーがない場合、値は NSURLSessionTransferSizeUnknown です。

そのif文を避けようとしましたか?そのチェックを使用しないファイルをダウンロードするときは、あなたが行った分割の進行状況のみを計算します。

于 2016-01-21T11:26:44.507 に答える
-1

参考になれば幸いです。多くのインジケーター タイプを使用できます https://github.com/jdg/MBProgressHUD

于 2016-01-21T11:39:53.233 に答える