I am making a call to Objective C from a PhoneGap application to perform functions with Dropbox.
The problem I am having is that my callback to JavaScript is firing before a file is downloaded from Dropbox to the phone’s local filesystem.
Here is my Objective C method that begins the file download from Dropbox –
- (void) restore:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* javaScript = nil;
NSLog(@"Dropbox restore method is executing");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *localPath = [documentsDirectory stringByAppendingPathComponent:@"PocketHealth-backup.bk"];
NSString *dropBoxFile = @"/PocketHealth-backup.bk";
[[self restClient] loadFile:dropBoxFile intoPath:localPath];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
javaScript = [pluginResult toSuccessCallbackString:command.callbackId];
[self writeJavascript:javaScript];
}
There is a method in Objective C that executes when the Dropbox file has been downloaded to the phone. I do get the NSLog that this method outputs after the Dropbox file download is complete. The problem is that I need to know when this event happens before I can return my JavaScript callback.
Here is the method that executes when the Dropbox file download is complete –
- (void) restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
{
NSLog(@"File loaded into path: %@", localPath);
}
How can I wait until the Dropbox file download is complete before returning the JavaScript callback that is in the restore method?
Instead of writing the JavaScript into a local variable inside your
restoremethod, add it as an iVar to your class, remove the call towriteJavascript:in yourrestoremethod and callwriteJavascriptfromrestClient:loadedFile:. Then it should get called when the download finished instead of when the actual download is started.