gpt4 book ai didi

objective-c - 使用 iOS Dropbox SDK 分块上传核心数据

转载 作者:可可西里 更新时间:2023-11-01 03:27:27 27 4
gpt4 key购买 nike

我有一个 iOS 应用程序,它使用 Core Data 进行持久数据存储。我集成了 Dropbox 作为用户执行持久存储文件 (appname.sqlite) 备份的一种方式。

UIButton 调用一个方法来查看文件是否已存在于 Dropbox 上:

            if([[DBSession sharedSession]isLinked])
{
NSString *folderName = [[self.dateFormatter stringFromDate:[NSDate date]] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
NSString *destinationPath = [NSString stringWithFormat:@"/GradeBook Pro/Backup/%@/",folderName];
self.metadataIndex = METADATA_REQUEST_BACKUP;
[self.restClient loadMetadata:destinationPath];
}

loadedMetadata 委托(delegate)方法使用现有文件的修订号(如果存在)启动上传。

-(void) restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata
{
SAVE_CORE_DATA;
NSString *folderName = [[self.dateFormatter stringFromDate:[NSDate date]] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
NSString *documentsDirectory = DOCUMENTS_DIRECTORY;
NSString *sourcePath = [NSString stringWithFormat:@"%@/GradeBookPro.sqlite", documentsDirectory];
NSString *destinationPath = [NSString stringWithFormat:@"/GradeBook Pro/Backup/%@/",folderName];
[self.restClient uploadFile:@"GradeBookPro.sqlite" toPath:destinationPath withParentRev:[[metadata.contents lastObject]rev] fromPath:sourcePath];
}

这对于相当小的文件或通过完美网络连接的大文件来说效果很好,但上传过程中的任何小错误都会取消整个过程。我想切换到使用分 block 上传方法,但我不知道如何实际执行 .sqlite 文件的“分 block ”。

我似乎找不到任何我可以从中学习的使用分 block 上传的示例应用程序,文档只是说以分 block 形式提供文件。

所以,我的问题是:

  1. 分 block 上传是否是解决用户通过可能不稳定的网络连接上传大文件问​​题的正确方法?

  2. 你能给我指出“分 block ”文件的示例代码/应用程序/文档吗?我对 Dropbox SDK 非常满意。

谢谢!

最佳答案

我会自己回答这个问题,以防其他人遇到同样的问题。

事实证明,我让这种方式变得比需要的更困难。 Dropbox SDK 处理文件分 block ,所以我只需要启动传输并对委托(delegate)调用使用react。使用的方法是:

要发送文件 block - 对于第一个 block ,使用 nil 作为 uploadId,使用 0 作为偏移量:

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

发送最后一个 block 后,使用此方法提交上传:

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

我按如下方式处理委托(delegate)方法:

    - (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]];
}
}

因为我试图解决的主要问题是处理不良连接,所以我为失败的 block 上传实现了委托(delegate)方法:

- (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
}
}

关于objective-c - 使用 iOS Dropbox SDK 分块上传核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com