cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How To Tell File Being Uploaded From Progress Block

How To Tell File Being Uploaded From Progress Block

Eric-H
Explorer | Level 3
Go to solution

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

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution
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.

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

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
Explorer | Level 3
Go to solution

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
Dropbox Staff
Go to solution
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.

Eric-H
Explorer | Level 3
Go to solution

Apologies - I was being dense.  I forgot that I had access to the file variable in the setProgress block.  I'm good now.

 

Many thanks!

 

Eric

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Eric-H Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?