dev-dom
dev-dom

Reputation: 379

How to use restclient dropbox API with NSURLSessionDownloadTask to download files

Problem: i want to download a file from my dropbox account and use quick look to visualize it.

First Solution:

1) use Dropbox API restClient:

[[self restClient] loadFile:fullpath intoPath:finalpath];

2) Once downloaded use QLPreviewController to preview the file.

The problem with this solution is that I don't know how to synchronize the download with the preview (to use quick look the file needs to be local, so I need to download it first).

The (ugly) workaround I came up with is to set up an alert ("Caching") and make it last an arbitrary length of time (let's say 12 sec, magic number...). At the same time I pause execution for 10-12 seconds (magic numbers):

[NSThread sleepForTimeInterval:12.0f];

...and hope at the end of this time interval the file is downloaded already so I can start the QLPreviewController.

Here is the code (ugly, I know....):

// Define Alert
UIAlertView *downloadAlert = [[UIAlertView alloc] initWithTitle:@"caching" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil] ;

// If  file does not exist alert downloading is ongoing
if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
{
    // Alert Popup
    [downloadAlert show];
    //[self performSelector:@selector(isExecuting) withObject:downloadAlert afterDelay:12];

}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Here your non-main thread.
    if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
    {
    [NSThread sleepForTimeInterval:12.0f];
    }

        dispatch_async(dispatch_get_main_queue(), ^{

        // Dismiss alert
        [downloadAlert dismissWithClickedButtonIndex: -1 animated:YES];

        //Here we return to main thread.
        // We use the QuickLook APIs directly to preview the document -
        QLPreviewController *previewController = [[QLPreviewController alloc] init];
        previewController.dataSource = self;
        previewController.delegate = self;
        // Push new viewcontroller, previewing the document
        [[self navigationController] pushViewController:previewController animated:YES];

    });
        });

It does work (with small files and fast connection) but It's not the best solution... .

I think that the best solution would be integrate NSURLSession with dropbox restClient so to use this routine:

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
                                                        delegate:nil
                                                   delegateQueue:[NSOperationQueue mainQueue]];
  NSURLSessionDownloadTask *task;
  task = [session downloadTaskWithRequest:request
                        completionHandler:^(NSURL *localfile, NSURLResponse *response, NSErr or *error) {
/* yes, can do UI things directly because this is called on the main queue */ }];
  [task resume];

But I'm not sure how to use it with the DropBox API: any suggestion ?

Thanks, dom

Upvotes: 1

Views: 628

Answers (1)

danh
danh

Reputation: 62676

It looks like the API tells you about progress and completion:

- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath contentType:(NSString*)contentType metadata:(DBMetadata*)metadata;
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath;

No need to do any sleeping or gcd calls directly. Just change your UI to show busy when the download starts and use these to update UI with progress and completion.

Upvotes: 1

Related Questions