2017-05-18 1 views
0

私は新しいのDropboxのSDKで大きなファイルをダウンロードしようとしています。はfilesRoutes' downloadUrlで新しいDropboxのSDK(V2)を使用して、進捗状況を追跡:上書き:先:メソッド

マイダウンロードコードがthis-

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クラスのloadMetadataメソッドを使用するために使用されるようなものです。今度は、この方法では

- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath 

そのうちの1つは他のいくつかのデリゲートメソッドをトリガーする

[restClient loadMetadata:path]; 

、私がダウンロードされているファイルの進行状況を追跡することができます。

どのようにしてはsetResponseブロックの私のダウンロード進行状況を追跡することができますか? ありがとうございます。

答えて

1

あなたは、進行状況の更新を受信するためにブロックを設定するためにsetProgressBlockを使用することができます。例はここにあります:

https://github.com/dropbox/dropbox-sdk-obj-c#download-style-request

+0

ありがとうございます。他の人の場合は、MBProgressHUDで視覚的に表示するために、パーセンテージにバイトを設定する方法のサンプルコードを追加することができます。ちょうど提案。どうもありがとう。私はそれをあなたの提案と共に働かせました。 – Natasha

0

メインクレジットはグレッグに行きます。私は視覚的にMBProgressHUDを使用しています

は、データ/画像/ファイルのダウンロードを示しています。

今、古いDropboxのSDKのDBRestClientデリゲートを削除した後、これは、私はそれがうまく作っています方法です。私の場合は

は、まず、私は、ドキュメントディレクトリに画像/ファイルをダウンロードしています以下のコード -

でのDropboxからファイルを一覧表示しています。

まず、ファイルとフォルダのリストが必要です。したがって、パスはドロップボックスのルートである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; 
} 

これは他にも役立ちます。私にヒントを与えてくださったグレッグに再び感謝します。

関連する問題