0

I want to make progress bar for api calling and end with success and i am using the AFNetworking 3.0 version.

I do the following code for measure the progress.

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

But method return from

if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown) return;

This statement always return true. I don't understand why? . I also print the header and it has "contact length" option.

Chirag Shah
  • 3,034
  • 1
  • 30
  • 61

3 Answers3

0

From the Apple Docs

Discussion This value is determined based on the Content-Length header received from the server. If that header is absent, the value is NSURLSessionTransferSizeUnknown.

Have you tried to avoid that if statement? When I download files I don't use that check, I only calculate the progress with the division you did.

Matt39
  • 149
  • 8
  • i try with comment that code then i got -1 in [dataTask countOfBytesExpectedToReceive]; this mothod – Chirag Shah Jan 21 '16 at 11:57
  • Try to have a look to this response [http://stackoverflow.com/a/21123613/5809921](http://stackoverflow.com/a/21123613/5809921) – Matt39 Jan 21 '16 at 13:41
  • I try with your solution link but i get expectedContentLength = -1 every time. Need to change at api side? – Chirag Shah Jan 21 '16 at 15:20
0

hmm, i use this method to monitor the download progress and it works fine for me.

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

↓↓↓↓↓↓ For Example ↓↓↓↓↓↓

Start Download:

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];

Calculate Download Progress:

__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;
    });

}];

Result:

TestProject Image hope it helps.

UtaSora
  • 1
  • 1
  • 2
  • I am not downloading any thing i just want to sync calendar event and get the response back. dont need to download anything – Chirag Shah Jan 21 '16 at 14:29
-1

I hope it help you, you can have a lot of indicator types https://github.com/jdg/MBProgressHUD

Ahmed Abdallah
  • 2,338
  • 1
  • 19
  • 30