0

Mailcoreには、添付ファイルをダウンロードし、ダウンロードの進行状況を返すパラメーターとしてブロックを受け入れるクールなメソッドがあります。

- (CTCoreAttachment *)fetchFullAttachmentWithProgress:(CTProgressBlock)block;

CTProgressBlock は次のように定義されます。

typedef void (^CTProgressBlock)(size_t curr, size_t max);

したがって、通常は次のように使用します。

//AttachmentDownloader.m
int fileNum = x; // explained later
CTCoreAttachment* fullAttachment = 
    [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
}];

問題は、この最後のメソッドがメインの UI クラスによって呼び出され、FileBucket.mこのクラスが多くの異なる UI 要素の多くの添付ファイルを順番に取得していることです。FileBucket.mこのコールバック メソッドで、この進行状況がどのアタッチメントに属しているかを報告したいと考えています。つまり、次のようなものが必要です。

// FileBucket.m
[AttachmentDownloader runMultiple:attachmentTree 
                     withProgress:^(size_t curr, size_t max, int fileNum) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
    NSLog(@"::: this progress belongs to element %d", fileNum);
}];

これは説明/図解するのが難しいことを知っています..もう1つの追加事項:この進行状況がどのアタッチメントに関するものかを認識しています..しかし、コールバックブロックが呼び出されるたびAttachmentDownloader.mにそれを戻したいだけです。FileBucket.m

4

2 に答える 2

0

私がこれを理解した方法は、それを2つのステップに分割することです。

  1. 委任を通じて私が望むものを実装する
  2. 委任をブロックベースのアプローチに変換します。

この問題は英語で説明するのがちょっと難しいので、サンプルコードで説明するのが一番だと思いました:)

委任ソリューション:

// AttachmentDownloader.m
+(void)runMultiple:(NSDictionary *)attachmentTree 
      withDelegate:(id<AttachmentDownloaderDelegate>)delegate  {
    ..

    CTCoreAttachment* fullAttachment = 
       [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
            NSLog(@"::: this is curr: %zu", curr);
            NSLog(@"::: this is max: %zu\n\n", max);
            NSLog(@"::: this is attachment uid %@", [message uid]);
            [delegate deliverProgressWithInfo:@{@"uid":[message uid],
                                                @"curr":[NSNumber numberWithInt:curr],
                                                @"max":[NSNumber numberWithInt:max]}];
     }];


// FileBucket.m
[AttachmentDownloader runMultiple:attachmentTree withProgress:^(size_t curr, size_t max) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
} withDelegate:self];

// delegate method
-(void)deliverProgressWithInfo:(NSDictionary *)dict {
    NSLog(@"filebucket: this is info being updated %@", dict);
}

ブロック ソリューション:

// AttachmentDownloader.m
+(void)runMultiple:(NSDictionary *)attachmentTree 
      withProgress:(void(^)(NSDictionary *))block {
    ..

    CTCoreAttachment* fullAttachment = 
        [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
        NSLog(@"::: this is curr: %zu", curr);
        NSLog(@"::: this is max: %zu\n\n", max);
        NSLog(@"::: this is attachment uid %@", [message uid]);
        block(@{@"uid":  [message uid],
                @"curr": [NSNumber numberWithInt:curr],
                @"max":  [NSNumber numberWithInt:max]});
    }];


// FileBucket.m
-(void)downloadSelectedAttachments {
    NSDictionary* attachmentTree = [self getAttachmentTree];
    [AttachmentDownloader runMultiple:attachmentTree withProgress:^(NSDictionary * dict) {
        NSLog(@"filebucket: this is info being updated %@", dict);
    }];
}
于 2013-07-22T05:59:17.583 に答える