使用带有 filesRoutes 的 downloadUrl:overwrite:destination: 方法的新 Dropbox SDK (V2) 跟踪进度
Track the progress using new Dropbox SDK (V2) with filesRoutes' downloadUrl:overwrite:destination: method
我正在尝试使用新的 Dropbox SDK 下载一个大文件。
我的下载代码是这样的-
DBUserClient *client = [DBClientsManager authorizedClient];
[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
if(result){
NSLog(@"File Downloaded");
// open the file after being downloaded
}
}];
以前我用的是DBRestClient class'的loadMetadata方法。
[restClient loadMetadata:path];
这又会触发其他一些委托方法,其中之一是
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
在这种方法中,我可以跟踪正在下载的文件的进度。
如何在 setResponse 块中跟踪我的下载进度?
提前致谢。
您可以使用setProgressBlock
设置一个块来接收进度更新。这里有一个例子:
https://github.com/dropbox/dropbox-sdk-obj-c#download-style-request
主要归功于 Greg。
我正在使用 MBProgressHUD 直观地显示 data/image/file 下载。
现在,在删除旧的 Dropbox SDK 的 DBRestClient 委托后,这就是我让它工作的方式。
就我而言,首先,我使用以下代码列出保管箱中的文件-
我正在下载 image/File 到文档目录。
为此,首先,我需要文件和文件夹列表,因此,我通过调用 getAllDropboxResourcesFromPath:
方法获取列表,其中路径是保管箱的根目录。
在我的 viewDidLoad 方法中,我正在获取列表。
- (void)viewDidLoad
{
[super viewDidLoad];
[self getAllDropboxResourcesFromPath:@"/"];
self.list = [NSMutableArray alloc]init; //I have the list array in my interface file
}
-(void)getAllDropboxResourcesFromPath:(NSString*)path{
DBUserClient *client = [DBClientsManager authorizedClient];
NSMutableArray *dirList = [[NSMutableArray alloc] init];
[[client.filesRoutes listFolder:path]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore){
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void)keepListingResources:(DBUserClient *)client cursor:(NSString *)cursor {
[[client.filesRoutes listFolderContinue:cursor]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError,
DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore) {
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void) listAllResources:(NSArray<DBFILESMetadata *> *)entries {
for (DBFILESMetadata *entry in entries) {
[dirList addObject:entry];
}
}
以上代码将所有文件和文件夹存储为列表数组中的 DBFILESMetadata
类型对象。
现在我可以下载了,但是我的文件很大,所以我需要显示我正在使用的下载进度 MBProgressHUD
。
-(void)downloadOnlyImageWithPngFormat:(DBFILESMetadata *)file{
//adding the progress view when download operation will be called
self.progressView = [[MBProgressHUD alloc] initWithWindow:[AppDelegate window]]; //I have an instance of MBProgressHUD in my interface file
[[AppDelegate window] addSubview:self.progressView];
//checking if the content is a file and if it has png extension
if ([file isKindOfClass:[DBFILESFileMetadata class]] && [file.name hasSuffix:@".png"]) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* localPath = [documentsPath stringByAppendingPathComponent:file.pathLower];
BOOL exists=[[NSFileManager defaultManager] fileExistsAtPath:localPath];
if(!exists) {
[[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *documentUrl = [NSURL fileURLWithPath:localPath];
NsString *remotePath = file.pathLower;
DBUserClient *client = [DBClientsManager authorizedClient];
[[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
if(result){
NSLog(@"File Downloaded");
}
}] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
[self setProgress:[self calculateProgress:totalBytesExpectedToDownload andTotalDownloadedBytes:totalBytesDownloaded]];
}];
}
- (void) setProgress:(CGFloat) progress {
[self.progressView setProgress:progress];
}
- (CGFloat) calculateProgress:(long long)totalbytes andTotalDownloadedBytes:(long long)downloadedBytes{
double result = (double)downloadedBytes/totalbytes;
return result;
}
希望这可以帮助到其他人。再次感谢 Greg 给我的提示。
我正在尝试使用新的 Dropbox SDK 下载一个大文件。
我的下载代码是这样的-
DBUserClient *client = [DBClientsManager authorizedClient];
[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
if(result){
NSLog(@"File Downloaded");
// open the file after being downloaded
}
}];
以前我用的是DBRestClient class'的loadMetadata方法。
[restClient loadMetadata:path];
这又会触发其他一些委托方法,其中之一是
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
在这种方法中,我可以跟踪正在下载的文件的进度。
如何在 setResponse 块中跟踪我的下载进度? 提前致谢。
您可以使用setProgressBlock
设置一个块来接收进度更新。这里有一个例子:
https://github.com/dropbox/dropbox-sdk-obj-c#download-style-request
主要归功于 Greg。
我正在使用 MBProgressHUD 直观地显示 data/image/file 下载。
现在,在删除旧的 Dropbox SDK 的 DBRestClient 委托后,这就是我让它工作的方式。
就我而言,首先,我使用以下代码列出保管箱中的文件-
我正在下载 image/File 到文档目录。
为此,首先,我需要文件和文件夹列表,因此,我通过调用 getAllDropboxResourcesFromPath:
方法获取列表,其中路径是保管箱的根目录。
在我的 viewDidLoad 方法中,我正在获取列表。
- (void)viewDidLoad
{
[super viewDidLoad];
[self getAllDropboxResourcesFromPath:@"/"];
self.list = [NSMutableArray alloc]init; //I have the list array in my interface file
}
-(void)getAllDropboxResourcesFromPath:(NSString*)path{
DBUserClient *client = [DBClientsManager authorizedClient];
NSMutableArray *dirList = [[NSMutableArray alloc] init];
[[client.filesRoutes listFolder:path]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore){
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void)keepListingResources:(DBUserClient *)client cursor:(NSString *)cursor {
[[client.filesRoutes listFolderContinue:cursor]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError,
DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore) {
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void) listAllResources:(NSArray<DBFILESMetadata *> *)entries {
for (DBFILESMetadata *entry in entries) {
[dirList addObject:entry];
}
}
以上代码将所有文件和文件夹存储为列表数组中的 DBFILESMetadata
类型对象。
现在我可以下载了,但是我的文件很大,所以我需要显示我正在使用的下载进度 MBProgressHUD
。
-(void)downloadOnlyImageWithPngFormat:(DBFILESMetadata *)file{
//adding the progress view when download operation will be called
self.progressView = [[MBProgressHUD alloc] initWithWindow:[AppDelegate window]]; //I have an instance of MBProgressHUD in my interface file
[[AppDelegate window] addSubview:self.progressView];
//checking if the content is a file and if it has png extension
if ([file isKindOfClass:[DBFILESFileMetadata class]] && [file.name hasSuffix:@".png"]) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* localPath = [documentsPath stringByAppendingPathComponent:file.pathLower];
BOOL exists=[[NSFileManager defaultManager] fileExistsAtPath:localPath];
if(!exists) {
[[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *documentUrl = [NSURL fileURLWithPath:localPath];
NsString *remotePath = file.pathLower;
DBUserClient *client = [DBClientsManager authorizedClient];
[[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
if(result){
NSLog(@"File Downloaded");
}
}] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
[self setProgress:[self calculateProgress:totalBytesExpectedToDownload andTotalDownloadedBytes:totalBytesDownloaded]];
}];
}
- (void) setProgress:(CGFloat) progress {
[self.progressView setProgress:progress];
}
- (CGFloat) calculateProgress:(long long)totalbytes andTotalDownloadedBytes:(long long)downloadedBytes{
double result = (double)downloadedBytes/totalbytes;
return result;
}
希望这可以帮助到其他人。再次感谢 Greg 给我的提示。