Track the progress using new Dropbox SDK (V2) with filesRoutes' downloadUrl:overwrite:destination: method

387 Views Asked by At

I am trying to download a large file with the new dropbox SDK.

My downloading code is something like this-

DBUserClient *client = [DBClientsManager authorizedClient];

[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {    
        if(result){
            NSLog(@"File Downloaded");
            // open the file after being downloaded
        }
    }];

Previously I used to use the the DBRestClient class' loadMetadata method.

[restClient loadMetadata:path];

which in turn would trigger the some other delegate methods, one of which is

- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath

in this method, I could track the progress of the file that is being downloaded.

How can I track my downloading progress in the setResponse Block? Thanks in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

You can use setProgressBlock to set a block to receive progress updates. There's an example here:

https://github.com/dropbox/dropbox-sdk-obj-c#download-style-request

0
On

Main credit goes to Greg.

I am using MBProgressHUD to visually show the data/image/file download.

Now, after removing the old dropbox SDK's DBRestClient delegates, this is how I am making it work.

In my case, First, I am listing the files from the dropbox with the following code-

I am downloading the image/File into the document directory.

For that, first, I need the files' and folders' list, So, I am getting the list by calling the getAllDropboxResourcesFromPath: method where path is the root of the dropbox.

in my viewDidLoad method, I am getting the list.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getAllDropboxResourcesFromPath:@"/"];
     self.list = [NSMutableArray alloc]init; //I have the list array in my interface file
}

-(void)getAllDropboxResourcesFromPath:(NSString*)path{

    DBUserClient *client = [DBClientsManager authorizedClient];

    NSMutableArray  *dirList = [[NSMutableArray alloc] init];

    [[client.filesRoutes listFolder:path]
     setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *networkError) {
         if (response) {
             NSArray<DBFILESMetadata *> *entries = response.entries;
             NSString *cursor = response.cursor;
             BOOL hasMore = [response.hasMore boolValue];

             [self listAllResources:entries];

             if (hasMore){
                 [self keepListingResources:client cursor:cursor];
             }
             else {
                 self.list = dirList;
             }
         } else {
             NSLog(@"%@\n%@\n", routeError, networkError);
         }
     }];
}

- (void)keepListingResources:(DBUserClient *)client cursor:(NSString *)cursor {
    [[client.filesRoutes listFolderContinue:cursor]
     setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError,
                        DBRequestError *networkError) {
         if (response) {
             NSArray<DBFILESMetadata *> *entries = response.entries;
             NSString *cursor = response.cursor;
             BOOL hasMore = [response.hasMore boolValue];

             [self listAllResources:entries];

             if (hasMore) {
                 [self keepListingResources:client cursor:cursor];
             }
             else {
                 self.list = dirList;
             }
         } else {
             NSLog(@"%@\n%@\n", routeError, networkError);
         }
     }];
}

- (void) listAllResources:(NSArray<DBFILESMetadata *> *)entries {
    for (DBFILESMetadata *entry in entries) {
           [dirList addObject:entry];    
    }
}

The above code stores all the files and folders as DBFILESMetadata type objects in the list array.

Now I am ready to download but my files are large, so I need to show the progress of the download for which I am using MBProgressHUD.

-(void)downloadOnlyImageWithPngFormat:(DBFILESMetadata *)file{

 //adding the progress view when download operation will be called
 self.progressView = [[MBProgressHUD alloc] initWithWindow:[AppDelegate window]]; //I have an instance of MBProgressHUD in my interface file
 [[AppDelegate window] addSubview:self.progressView];

 //checking if the content is a file and if it has png extension
 if ([file isKindOfClass:[DBFILESFileMetadata class]] && [file.name hasSuffix:@".png"]) {

        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* localPath = [documentsPath stringByAppendingPathComponent:file.pathLower];
        BOOL exists=[[NSFileManager defaultManager] fileExistsAtPath:localPath];
        if(!exists) {
            [[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSURL *documentUrl = [NSURL fileURLWithPath:localPath];
        NsString *remotePath = file.pathLower;
        DBUserClient *client = [DBClientsManager authorizedClient];
        [[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
        if(result){
            NSLog(@"File Downloaded");
        }
    }] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
        [self setProgress:[self calculateProgress:totalBytesExpectedToDownload andTotalDownloadedBytes:totalBytesDownloaded]];
    }];
}

- (void) setProgress:(CGFloat) progress {
   [self.progressView setProgress:progress];
}
- (CGFloat) calculateProgress:(long long)totalbytes andTotalDownloadedBytes:(long long)downloadedBytes{
    double result = (double)downloadedBytes/totalbytes;
    return result;
}

Hope this can help others. Again big thanks to Greg for giving me the hints.