Forum Discussion

sdas's avatar
sdas
Explorer | Level 3
8 years ago
Solved

How to implement loadedFile method in API V2

Here is  my code , how to convert this  into  API v2 compitable methods.

 

 

-(void)prepareForRecoverDB{
    if(![Utility isNetworkAvailable]) {
        [Utility showAlertViewWithTitle:@"Error!" Message:@"Unable to connect to the host. Please check your network connection." CancelTitle:@"OK"];
        return;
    }
    
    [self showLoadingView];
    
    DBMetadata *mData = [self.arrOfBackupFiles objectAtIndex:self.indexPath_.row];
    AppDelegate_Shared *appDelegate = (AppDelegate_Shared *)[UIApplication sharedApplication].delegate;
    DBRestClient *aClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    aClient.delegate = self;
    
    NSString *strPath = [appDelegate dbPath];
    NSArray *arrPath = [strPath componentsSeparatedByString:@"/"];
    NSString *lastPath = [arrPath lastObject];
    if([lastPath isEqualToString:mData.filename]){
        self.restoredFileName = @"RentTracker.sqlite";
        [aClient loadFile:mData.path intoPath:[appDelegate dbPath:self.restoredFileName]];
    }
    else{
        [aClient loadFile:mData.path intoPath:[appDelegate dbPath:mData.filename]];
        self.restoredFileName = mData.filename;
        
    }
}
 
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath {
    [self hideLoadingView];
    [Utility showAlertViewWithTitle:@"Restored!" Message:@"File Restored Sucessful!" CancelTitle:@"OK"];
    [[DBSession sharedSession] unlinkAll];
    AppDelegate_Shared *app = (AppDelegate_Shared *)[UIApplication sharedApplication].delegate;
 
    NSFileManager *fm = [NSFileManager defaultManager];
    [fm removeItemAtPath:[app dbPath] error:nil];
    
    [[NSUserDefaults standardUserDefaults] setValue:self.restoredFileName forKey:kStoreName];
    [[NSUserDefaults standardUserDefaults] synchronize];
 
    app.managedObjectModel = nil;
    app.managedObjectContext = nil;
    app.persistentStoreCoordinator = nil;
    
    [self.navigationController popViewControllerAnimated:YES];
}
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    If you're using the API v2 Objective-C SDK (which is recommended), the equivalent of the loadFile method would be any of the download methods.

     

    You can find a sample of calling those here:

     

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

     

    As well as in the example app:

     

    https://github.com/dropbox/dropbox-sdk-obj-c/blob/master/Examples/DBRoulette/iOS/CocoaPodsProject/DBRoulette/DBRoulette/PhotoViewController.m#L121

    • sdas's avatar
      sdas
      Explorer | Level 3

      Hi, Greg 

         while  i am downloading the file path of  sqlite database , it's actually returing nil and  in error part it showing :-

       

      DropboxHttpError[{
          ErrorContent = "path/not_found/..";
          RequestId = 9c2085907ba4e2bda71a08d96d62af8b;
          StatusCode = 409;
          UserMessage = nil;
      }];
      

       

       This is  my code for downloading data  from sqlite data path -----

       

       

      -(void)prepareForRecoverDataBase
      {
          if(![Utility isNetworkAvailable]) {
              [Utility showAlertViewWithTitle:@"Error!" Message:@"Unable to connect to the host. Please check your network connection." CancelTitle:@"OK"];
              return;
          }
          
          [self showLoadingView];
         // NSString *filename = [self.arrOfBackupFiles objectAtIndex:self.indexPath_.row];
          DBFILESFileMetadata *fileMetadata= [self.arrOfBackupFiles objectAtIndex:self.indexPath_.row];
          AppDelegate_Shared *appDelegate = (AppDelegate_Shared *)[UIApplication sharedApplication].delegate;
          NSString *strPath = [appDelegate dbPath];
          NSArray *arrPath = [strPath componentsSeparatedByString:@"/"];
          NSString *lastPath = [arrPath lastObject];
          
          if([lastPath isEqualToString:fileMetadata.name]){
              self.restoredFileName = @"RentTracker.sqlite";
              [appDelegate dbPath:self.restoredFileName];
          }
          else
          {
              [appDelegate dbPath:fileMetadata.name];
              self.restoredFileName=fileMetadata.name;
          }
          NSLog(@"currentPath %@",[appDelegate dbPath:fileMetadata.name]);
          
          DBUserClient *client = [DBClientsManager authorizedClient];
          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSURL *outputDirectory = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
          NSURL *outputUrl = [outputDirectory URLByAppendingPathComponent:fileMetadata.name];
          
          [[[client.filesRoutes downloadUrl:fileMetadata.pathDisplay overwrite:YES destination:outputUrl]
            setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError,
                               NSURL *destination) {
                if (result) {
                    NSLog(@"%@\n", result);
                    NSData *data = [[NSFileManager defaultManager] contentsAtPath:[destination path]];
                    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                    NSLog(@"%@\n", dataStr);
                } else {
                    NSLog(@"%@\n%@\n", routeError, networkError);
                }
            }] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
                NSLog(@"%lld\n%lld\n%lld\n", bytesDownloaded, totalBytesDownloaded, totalBytesExpectedToDownload);
            }];
          
       
        //  [self downloadData:[appDelegate dbPath:fileMetadata.name]];
          [self hideLoadingView];
          [Utility showAlertViewWithTitle:@"Restored!" Message:@"File Restored Sucessful!" CancelTitle:@"OK"];
          //    NSFileManager *fm = [NSFileManager defaultManager];
          //    [fm removeItemAtPath:[appDelegate dbPath] error:nil];
          
          [[NSUserDefaults standardUserDefaults] setValue:self.restoredFileName forKey:kStoreName];
          [[NSUserDefaults standardUserDefaults] synchronize];
          
      //    appDelegate.managedObjectModel = nil;
      //    appDelegate.managedObjectContext = nil;
      //    appDelegate.persistentStoreCoordinator = nil;
          
          [self.navigationController popViewControllerAnimated:YES];
      }

       

      • sdas's avatar
        sdas
        Explorer | Level 3

        So i thought may be i had mistaken something during  uploading  the  data.  Below is the  code for uploading the  sqlite dataPath  to dropbox.

        DB Path- 

        Before adding time stamp

        /var/mobile/Containers/Data/Application/6D7A6DDA-45D0-4509-99A5-E635891D9DB9/Documents/RentTracker.sqlite

         

        After adding time stamp

        /var/mobile/Containers/Data/Application/AA188B71-1995-42A1-AC39-64AFD024E42A/Documents/RentTracker_IOS_01_02_2017_10_46_58.sqlite

        So,what i am doing  is , converting the db file path into  data format  and  passing this  as  an input data [parameter], while  in uploadData [parameter],  i  sent   only the  file name . Please correct me , if i doing something wrong  in code . HELP ME ! 

        Thank You

         

         

        -(void)BackupData
        {
           // [[NSNotificationCenter defaultCenter]postNotificationName:kShowLoading object:self ];
           // AppDelegate *Maindelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [MBProgressHUD showHUDAddedTo:self.window animated:YES];
            
            AppDelegate_Shared *appDelegate = (AppDelegate_Shared *)[UIApplication sharedApplication].delegate;
            NSString *srcpath = [appDelegate dbPath];  // returning the sqlite file path 
           /*  Adding time stamp  */ 
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"MM_dd_yyyy_hh_mm_ss"];
            NSString *dateStr = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"RentTracker_IOS_%@.sqlite", dateStr];
            DBUserClient *client = [DBClientsManager authorizedClient];
            NSData *fileData = [srcpath dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
            
           // [client.filesRoutes uploadData:@"/" inputData:fileData]; //extra
            // For overriding on upload
            DBFILESWriteMode *mode = [[DBFILESWriteMode alloc] initWithOverwrite];
            // [NSString stringWithFormat:@"/%@",fileName]
            [[[client.filesRoutes uploadData: [NSString stringWithFormat:@"/%@",fileName]
                                        mode:mode
                                  autorename:@(YES)
                              clientModified:nil
                                        mute:@(NO)
                                   inputData:fileData]
              setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *networkError) {
                  if (result) {
                      NSLog(@"%@\n", result);
                      NSLog(@"stringPath of the file %@",result.pathDisplay);
                      [[NSUserDefaults standardUserDefaults]setValue:result.pathDisplay forKey:@"path"];
                      [[NSUserDefaults standardUserDefaults]synchronize];
                    
                     [MBProgressHUD hideHUDForView:self.window animated:YES];
                      [Utility showAlertViewWithTitle:@"Backup!" Message:@"Backup was successful" CancelTitle:@"OK"];
                      [DBClientsManager unlinkAndResetClients];
                  } else {
                      NSLog(@"%@\n%@\n", routeError, networkError);
                  
                       [MBProgressHUD hideHUDForView:self.window animated:YES];
                  }
              }] setProgressBlock:^(int64_t bytesUploaded, int64_t totalBytesUploaded, int64_t totalBytesExpectedToUploaded) {
                  NSLog(@"\n%lld\n%lld\n%lld\n", bytesUploaded, totalBytesUploaded, totalBytesExpectedToUploaded);
                 
              }];
            [formatter release]; formatter = nil;
            
        }

         

         

         

    • Greg-DB's avatar
      Greg-DB
      Icon for Dropbox Staff rankDropbox Staff

      (I was writing this in response to your new messages, but it looks like you either deleted them or there's an issue with the forum not displaying them.)

       

      The 'path/not_found' error indicates that there's nothing at the path you specified. When referencing paths in Dropbox, you should use the remote path, not the local path of the file. For example, if you uploaded a file named "RentTracker.sqlite" to root, the path you would need to download it would just be "/RentTracker.sqlite". 

       

      You can get the paths of uploaded files by listing the contents of folders using the listFolder methods. E.g., to list the root, supply the path as empty string "". (You can also manually check the contents of your own account on the Dropbox web site.) You should use the returned pathLower values to reference paths in other API calls.

       

      Note that if your app has the "app folder" permission, all of these paths will automatically be relative to the app's app folder (by default inside "/Apps". Your app doesn't need to add that manually though.)

      • sdas's avatar
        sdas
        Explorer | Level 3

        Hi Greg,

        Thanks for your valuable reply. As you suggested me to upload the root file path.  But i am still confused in uploading the data. so what should i  put in  uploadData:?? parameter  and  what should  be in inputData?? parameter. Thank You.

        [[[client.filesRoutes uploadData:@"???"
                                    mode:mode
                              autorename:@(YES)
                          clientModified:nil
                                    mute:@(NO)
                               inputData:???]
            setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *networkError) {
              if (result) {
                NSLog(@"%@\n", result);
              } else {
                NSLog(@"%@\n%@\n", routeError, networkError);
              }
            }] setProgressBlock:^(int64_t bytesUploaded, int64_t totalBytesUploaded, int64_t totalBytesExpectedToUploaded) {
          NSLog(@"\n%lld\n%lld\n%lld\n", bytesUploaded, totalBytesUploaded, totalBytesExpectedToUploaded);
        }];