We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
C. Logan
9 years agoExplorer | Level 3
Uploading big file using Objective-C API V2 very SLOW...
Hello, recently our app updates the Dropbox related code from API V1 to API V2. Usually we need to transfer files to Dropbox, it is OK for small files, but for video files of size about 1.5 GB th...
Greg-DB
Dropbox Community Moderator
9 years agoHi Logan, are you using a chunk size of 32768 bytes? You should probably use something much larger than that, at least 4 MB, or even significantly larger than that.
The optimal size depends on a variety of factors, so you may want to try a few different ones and see what works best for your app. Using a small chunk size can be useful, as it minimizes how much data you need to re-upload for failed requests, and reduces the chance of any particular request of failing, but it causes more overhead. A larger chunk size can reduce the overhead, which can improve overall transfer speed.
- C. Logan9 years agoExplorer | Level 3Hi Greg,
thanks for reply!
1. I have tried to pass the chunk of data with size 5 MB and 100 MB, but the result was the same. It seems that the bytesWritten from the progress block is always 32768 and in few cases also 65536, as shown in the log message. How can I modify the data chunk size for each transfer? Currently I use the following code:
NSData *data = [dropboxObj.fileHandle readDataOfLength:length]; // length = 5 MB or 100 MB or other values
uploadTask = [client.filesRoutes uploadSessionStartData:data];
2. Just a few minutes ago I tried to upload again, but there is no message from the progress block, and after one or two minutes the operation is canceled. My network is normal. So I guess there might be something wrong with the connection to Dropbox, or is there still anything in API V2 that I should know
when doing the upload ?
Thanks for your attention!
Logan- Greg-DB9 years ago
Dropbox Community Moderator
1. The chunk size should just be the size of the data you supply for any given call, and that's what should get reported back in the progress callback, so it's unexpected that you'd still get 32768. Can you share the rest of the relevant code, including the definition of length, the progresc callback definitifion, etc.?
2. That also doesn't sound like the expected behavior. If you share the rest of the code and the steps to reproduce it we'll be happy to look into it.
- C. Logan9 years agoExplorer | Level 3
// My code can be summarized as follows:
// sorry for the indentation, it seems the code indentation is gone.
const NSUInteger DB_CHUNK_SIZE = 5*1024*1024;
DropboxClient *client = [DropboxClientsManager authorizedClient];
// create dropboxObj to pass parameters
TGDropboxObject *dropboxObj = [[TGDropboxObject alloc] init];
dropboxObj.progressBlock = progress;
dropboxObj.completionBlock = completion;
dropboxObj.fileSize = [self getFileSize:localPath fromInternal:fromInternal];
dropboxObj.chunkSize = DB_CHUNK_SIZE;
dropboxObj.curTotalBytes = 0;
dropboxObj.srcPath = localPath;
dropboxObj.dstPath = remotePath;
dropboxObj.fromInternal = fromInternal;
if (fromInternal) {
dropboxObj.fileHandle = [NSFileHandle fileHandleForReadingAtPath:localPath];
}
// skip……NSData *data = [self readDataOfLength:dropboxObj.chunkSize fromObj:dropboxObj];// read one chunk of data
DBUploadTask<DBFILESUploadSessionStartResult *, DBNilObject *> *uploadTask;
uploadTask = [client.filesRoutes uploadSessionStartData:data];
[uploadTask response:^(DBFILESUploadSessionStartResult * _Nullable result, DBNilObject * _Nullable obj, DBRequestError * _Nullable requestError) {
if (result) { // success for the first chunk
dropboxObj.sessionId = result.sessionId;
DBFILESUploadSessionCursor *cursor = [[DBFILESUploadSessionCursor alloc] initWithSessionId:result.sessionId offset:[NSNumber numberWithUnsignedLongLong:[self getOffsetFromObj:dropboxObj]]];
[self uploadNextFromObj:dropboxObj cursor:cursor];// do upload for subsequent chunks
}
else { // failure
[self closeFileFromObj:dropboxObj];// skip…
dropboxObj.completionBlock(nil, error);
}
}];// progress block for uploadTask
[uploadTask progress:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
dropboxObj.curTotalBytes += bytesWritten;
CGFloat progress = (CGFloat)dropboxObj.curTotalBytes / (CGFloat)dropboxObj.fileSize;
NSLog(@"start: bytesWritten = %lld progress = %.4f", bytesWritten, progress);
dropboxObj.progressBlock(progress);
}];// The method for uploading subsequent chunks:
//-------------------------------------------------------
- (void)uploadNextFromObj:(TGDropboxObject *)dropboxObj cursor:(DBFILESUploadSessionCursor *)cursor {
DropboxClient *client = [DropboxClientsManager authorizedClient];
NSData *const chunk = [self readDataOfLength:dropboxObj.chunkSize fromObj:dropboxObj];
const BOOL isLastChunk = chunk.length < dropboxObj.chunkSize;
if (isLastChunk == NO) {
DBUploadTask<DBNilObject *, DBFILESUploadSessionLookupError *> *continueTask;
continueTask = [client.filesRoutes uploadSessionAppendV2Data:cursor inputData:chunk];
[continueTask response:^(DBNilObject * _Nullable obj, DBFILESUploadSessionLookupError * _Nullable lookupError, DBRequestError * _Nullable requestError) {
if (lookupError == nil) { // success
DBFILESUploadSessionCursor *cursor = [[DBFILESUploadSessionCursor alloc] initWithSessionId:dropboxObj.sessionId offset:[NSNumber numberWithUnsignedLongLong:[self getOffsetFromObj:dropboxObj]]];
[self uploadNextFromObj:dropboxObj cursor:cursor];} else { // failure
[self closeFileFromObj:dropboxObj];
// skip…dropboxObj.completionBlock(nil, error);
}
}];// progress block for continueTask
[continueTask progress:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
dropboxObj.curTotalBytes += bytesWritten;
CGFloat progress = (CGFloat)dropboxObj.curTotalBytes / (CGFloat)dropboxObj.fileSize;
NSLog(@"continue: bytesWritten = %lld progress = %.4f", bytesWritten, progress);
dropboxObj.progressBlock(progress);
}];
}
else { // last chunk
DBUploadTask<DBFILESFileMetadata *, DBFILESUploadSessionFinishError *> *finishTask;
DBFILESCommitInfo *info = [[DBFILESCommitInfo alloc] initWithPath:dropboxObj.dstPath];
finishTask = [client.filesRoutes uploadSessionFinishData:cursor commit:info inputData:chunk];
[finishTask response:^(DBFILESFileMetadata * _Nullable metaData, DBFILESUploadSessionFinishError * _Nullable finishError, DBRequestError * _Nullable requestError) {
DLog(@"metaData: %@", metaData);
[self closeFileFromObj:dropboxObj];
// skip…dropboxObj.completionBlock(metaData, error);
}];
[finishTask progress:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
dropboxObj.curTotalBytes += bytesWritten;
CGFloat progress = (CGFloat)dropboxObj.curTotalBytes / (CGFloat)dropboxObj.fileSize;
NSLog(@"finish: bytesWritten = %lld progress = %.4f", bytesWritten, progress);
dropboxObj.progressBlock(progress);
}];
}
}// If there is anything unclear, just tell me, I can provide further infomation.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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, Facebook or Instagram.
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!