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