1

I am using Dropbox Sync API sdk it display all folder and file but we can not download file i am using this code:

  DBFilesystem *filesystem;
  DBFile *file = [_filesystem openFile:info.path error:nil];
   NSData *imagedata=[file readData:nil];
  [fileMan createFileAtPath:Image_filePath  contents:**data** attributes:nil];
  NSLog(@"flao=%d",[file writeData:imagedata error:nil]);
  // it return 1

I am getting NSData but i can't store in document directory. I check below links but no success

Dropbox Sync API iOS copy files to app documents

Data syncing with DropBox API and iOS

Please Help me.

Community
  • 1
  • 1
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • have you implemented below function and are you getting any values in below method my friend??? - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata { } – NiravPatel Sep 27 '13 at 11:40
  • @NiravPatel restclient is core api function so this function is allowed in sync app – kirti Chavda Sep 27 '13 at 11:42
  • Try to check errors. `NSError *error; NSData *imagedata = [file readData:&error]; if (error) NSLog(@"%@", error.localizedDescription);` – Azat Sep 27 '13 at 12:43

1 Answers1

1

Dropbox takes time to cache a file, you can set up a block to monitor when the cache is completed:

__block MyViewController *me = self;
[self.dropboxFile addObserver:self block:^() { [me monitorDropboxFile:@"Sync"]; }];

inside the observer:

- (void)monitorDropboxFile:(NSString *)caller
{
    if (self.dropboxFile.status.cached) {
        ; // Good to go, the file is cached
    } else {
        ; // download in progress
    }
}

Or, there is a simpler way, use readHandle:

(DBFile *)file;
// ...  
DBError *dbError;
NSFileHandle *fileHandle = [file readHandle:&dbError];

As documented in the Dropbox API header:

/** Returns a read-only file handle for the file. If the file is not cached then the method will block until the file is downloaded.
@return A file handle if the file can be read, or nil if an error occurred. */

The file handle is ready when returned. However, you might like to put readHandle in a background thread if the app has to be responsive during file download.

ohho
  • 50,879
  • 75
  • 256
  • 383