Using iOS Dropbox SDK to do a Chunked Upload of Core Data

纵饮孤独 提交于 2019-12-03 00:29:35

I'm going to answer this myself just in case anyone else has the same issue.

It turns out that I was making this way more difficult than it needed to be. The Dropbox SDK handles chunking the files so I just needed to initate the transfer and react to the delegate calls. The methods used are:

To send a file chunk - for the first chunk, use nil for the uploadId and 0 for the offset:

- (void)uploadFileChunk:(NSString *)uploadId offset:(unsigned long long)offset fromPath:(NSString *)localPath;

After sending the last chunk, use this method to commit the upload:

- (void)uploadFile:(NSString *)filename toPath:(NSString *)parentFolder withParentRev:(NSString *)parentRev fromUploadId:(NSString *)uploadId;

I handled the delegate method as follows:

    - (void)restClient:(DBRestClient *)client uploadedFileChunk:(NSString *)uploadId newOffset:(unsigned long long)offset fromFile:(NSString *)localPath expires:(NSDate *)expiresDate
    {
        unsigned long long fileSize = [[[NSFileManager defaultManager]attributesOfItemAtPath:[FileHelper localDatabaseFilePath] error:nil]fileSize];

        if (offset >= fileSize)
        {
            //Upload complete, commit the file.
            [self.restClient uploadFile:DATABASE_FILENAME toPath:[FileHelper remoteDatabaseDirectory] withParentRev:self.databaseRemoteRevision fromUploadId:uploadId];
        }
        else
        {
            //Send the next chunk and update the progress HUD.
            self.progressHUD.progress = (float)((float)offset / (float)fileSize);
            [self.restClient uploadFileChunk:uploadId offset:offset fromPath:[FileHelper localDatabaseFilePath]];
        }
    }

Since the main problem that I was trying to address was handling poor connections I implemented delegate method for failed chunk uploads:

- (void)restClient:(DBRestClient *)client uploadFileChunkFailedWithError:(NSError *)error
{
    if (error != nil && (self.uploadErrorCount < DROPBOX_MAX_UPLOAD_FAILURES))
    {
        self.uploadErrorCount++;
        NSString* uploadId = [error.userInfo objectForKey:@"upload_id"];
        unsigned long long offset = [[error.userInfo objectForKey:@"offset"]unsignedLongLongValue];
        [self.restClient uploadFileChunk:uploadId offset:offset fromPath:[FileHelper localDatabaseFilePath]];
    }
    else
    {
      //show an error message and cancel the process
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!