Forum Discussion

Eric-H's avatar
Eric-H
Explorer | Level 3
8 years ago
Solved

How To Tell File Being Uploaded From Progress Block

Is there any way to know what file is being worked on when the progress block is called?  See the working code below - I'd like to be able to put an update on the screen so that the user knows which file is being uploaded.  I know that it could be uploading several files synchronously - so this is why I'd like to know which one it is referring to each time the progress block is called.

 

            [[[dbClient.filesRoutes uploadUrl:remoteFile inputUrl:[NSURL fileURLWithPath:localFile]] setResponseBlock:^(DBFILESFileMetadata *metadata, DBFILESUploadError *uploadError, DBRequestError *error)
            {
                if (metadata)
                {
                    NSLog(@"The upload completed successfully.");
                    NSLog(@"File metadata:");
                    NSLog(@"%@", metadata);
                } else if (uploadError)
                {
                    NSLog(@"Something went wrong with the upload:");
                    NSLog(@"%@", uploadError);
                } else if (error)
                {
                    NSLog(@"Something went wrong with the API call:");
                    NSLog(@"%@", error);
                }
                
                ++self.filesUploaded;
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_FILE_UPLOADED object:self];
            }] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
            {
                float percentDone = (float)totalBytesWritten/(float)totalBytesExpectedToWrite*(float)100;
                self.alert.message = $str(@"Uploading File %0.0f%%\n\n",percentDone);
            }];

Thank you,

Eric

  • Thanks for following up Eric. I'm not sure if I understand your concern exactly though. It looks like the technique I described should work in your case.

    Your updateFileStatusPercent method takes the place of progressCallback in my sample above. You can add an extra parameter to updateFileStatusPercent to identify the specific file, like how I have the identifier parameter in my sample.

    For example, your identifier may be your NSString* file. The progress block is defined separately for each file, that is, for each iteration of your loop, so the identifier will be different each time. The subsequent calls to updateFileStatusPercent will then contain the distinct identifiers.
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Hi Eric, yes, you can disambiguate the different progress blocks by setting a variable inside each. You can then factor out the rest of the handling if you want. For example:

     

    - (void)progressCallback:(NSString*)identifier bytesWritten:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    
        NSLog(@"%@: bytesWritten: %lld, totalBytesWritten: %lld, totalBytesExpectedToWrite: %lld", identifier, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
    
    }

    ...

              }] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
             {
                 NSString *identifier = @"this is #1"; // or whatever you want
                 [self progressCallback:identifier bytesWritten:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
    
             }];

    ...

              }] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
             {
                 NSString *identifier = @"this is #2"; // or whatever you want
                 [self progressCallback:identifier bytesWritten:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
             }];

    Hope this helps! 

    • Eric-H's avatar
      Eric-H
      Explorer | Level 3

      Thanks for the quick reply Greg.  But I don't think that's what I'm looking for.  Apologies for not being more clear.  As you can see below, I'm calling uploadUrl in a loop - for each file that needs to be uploaded.  Therefore the setProgressBlock is getting called for each of the files being uploaded synchronously.  I'd like to know for which file the progress information is for.  Any way to know this?

       

      -(void) uploadLocalChanges
      {
          self.uploadLocalChangesCalled = YES;
          DBUserClient *dbClient = [DBClientsManager authorizedClient];
          NSMutableSet* allLocalFiles = [NSMutableSet set];
          [allLocalFiles addObjectsFromArray:self.localFiles.allKeys];
          
          for (NSString* file in allLocalFiles)
          {
              NSDate* localDate = [self.localFiles objectForKey:file];
              NSDate* remoteDate = [self.remoteFiles objectForKey:file];
              NSDate* lastSyncLocalDate = [self.lastSyncLocalFiles objectForKey:file];
              
              if (remoteDate == nil || (lastSyncLocalDate != nil && localDate > lastSyncLocalDate))
              {
                  ++self.filesToUpload;
                  //NSLog(@"Uploading file %@",file);
                  NSString* remoteFile = [@"/" stringByAppendingString:file];
                  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                  NSString* appPath = [[paths objectAtIndex:0] stringByAppendingString:@"/"];
                  NSString* localFile = [appPath stringByAppendingString:file];
                  
                  [[[dbClient.filesRoutes uploadUrl:remoteFile inputUrl:[NSURL fileURLWithPath:localFile]] setResponseBlock:^(DBFILESFileMetadata *metadata, DBFILESUploadError *uploadError, DBRequestError *error)
                  {
                      if (metadata)
                      {
                          /*
                          NSLog(@"The upload completed successfully.");
                          NSLog(@"File metadata:");
                          NSLog(@"%@", metadata);
                          */
                      } else if (uploadError)
                      {
                          NSLog(@"Something went wrong with the upload:");
                          NSLog(@"%@", uploadError);
                      } else if (error)
                      {
                          NSLog(@"Something went wrong with the API call:");
                          NSLog(@"%@", error);
                      }
                      
                      ++self.filesUploaded;
                      [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_FILE_UPLOADED object:self];
                  }] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
                  {
                      [self updateFileStatusPercent:@"Uploading ..." totalBytesWriten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
                      // Here we can monitor the progress of the transfer:
                      //NSLog(@"bytesWritten: %lld, totalBytesWritten: %lld, totalBytesExpectedToWrite: %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
                  }];
              }
          }
          if (self.filesToUpload == 0)
              [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_FILE_UPLOADED object:self];
      }

      Thank you,

      Eric

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff
        Thanks for following up Eric. I'm not sure if I understand your concern exactly though. It looks like the technique I described should work in your case.

        Your updateFileStatusPercent method takes the place of progressCallback in my sample above. You can add an extra parameter to updateFileStatusPercent to identify the specific file, like how I have the identifier parameter in my sample.

        For example, your identifier may be your NSString* file. The progress block is defined separately for each file, that is, for each iteration of your loop, so the identifier will be different each time. The subsequent calls to updateFileStatusPercent will then contain the distinct identifiers.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,951 PostsLatest Activity: 43 minutes ago
352 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!