0

我正在使用反应可可从服务器创建下载文件。我有一个带有进度属性(进度值、currentBytes、totalBytes)的DownloadMapFileOperation.m文件。RACSignal它在我的下一个方法和浮动进度中变化非常迅速(这是不必要的,但是

@property (nonatomic, strong, readwrite) NSMutableDictionary *progressDictionary;
@property (nonatomic, assign, readwrite) float progress;

RACSignal创建方法

- (RACSignal *)signalForDownloadMap:(Place *)place
{
    return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSString *mapUrlString = [NSString stringWithFormat:@"http://myurl/%@", place.mapFileName];
        NSURL *url = [NSURL URLWithString:mapUrlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        _downloadFileOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[url lastPathComponent]];

        [_downloadFileOperation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];

        [_downloadFileOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation , id response)
         {
             NSLog(@"Downloaded via compl block");
             [subscriber sendNext:nil];
             [subscriber sendCompleted];

         } failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             [subscriber sendError:error];
         }];

        @weakify(self)
        self.progressDictionary = [[NSMutableDictionary alloc] init];
        self.progressDictionary[@"progress"] = @(0);
        self.progressDictionary[@"current"] = @(0);
        self.progressDictionary[@"total"] = @(0);
        self.progress = @(0);

        [_downloadFileOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
         {
             @strongify(self)
             self.progressDictionary[@"current"] = @(totalBytesRead);
             self.progressDictionary[@"total"] = @(totalBytesExpectedToRead);
             self.progressDictionary[@"progress"] = @((float)totalBytesRead / (float)totalBytesExpectedToRead);
             self.progress = @((float)totalBytesRead / (float)totalBytesExpectedToRead);
             self.progress = @((float)totalBytesRead / (float)totalBytesExpectedToRead);
         }];

        [_downloadFileOperation start];
        return nil;
    }] filter:^BOOL(id value) {
        return !(.mapFileName == nil);
    }]
        deliverOn:[RACScheduler mainThreadScheduler]
    ];
}

我只想通过RACObserve我的 ViewController 在我的视图中显示进度。

[[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place]
     subscribeNext:^(AFHTTPRequestOperation *operation) {
        NSString *alertMessage = @"Download complete";
        [[[UIAlertView alloc] initWithTitle:@"Complete" message:alertMessage delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
        downloadMapView.progressLabel.text = @"Download Complete";
    }];

    [[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place] subscribeError:^(NSError *error) {
        [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
    }];

    RAC(downloadMapView, progressView.progress) = [RACObserve([DownloadMapFileOperation sharedInstance], progress)
                                                   map:^id(id value) {
                                                       return value;
                                                   }];

  RAC(downloadMapView, progressLabel.text) = [RACObserve([DownloadMapFileOperation sharedInstance], progressDictionary)
                                                    map:^id(NSDictionary *value) {
                                                        return [value[@"progress"] isEqual: @(1)] ? @"Download Complete" : [NSString stringWithFormat:@"Downloaded %@ of %@", value[@"current"], value[@"total"]];
                                                    }];

但是我的进度视图有时会从 60% 跳到 50%(或 45 - 35 等),并NSLog(@"Downloaded via compl block");在进度达到 100% 之前显示(有时它会从大约 70% 达到并再次开始)。如果我使用progressDictionary(如第二个RACObserve) - 它不起作用(显示 0 进度和已下载 0 of 0)

已编辑

我还尝试在我的DownloadMapFileOperation.m文件中添加暂停、恢复、取消下载操作的方法。

- (RACSignal *)signalForPauseDownloadMap
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [_downloadFileOperation pause];
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        return nil;
    }];
}

- (RACSignal *)signalForResumeDownloadMap
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [_downloadFileOperation resume];
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        return nil;
    }];
}

- (RACSignal *)signalForCancelDownloadMap
{
    return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [_downloadFileOperation cancel];
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        return nil;
    }] filter:^BOOL(id value) {
        return (_downloadFileOperation);
    }];
}

但是:我的下载文件操作并没有停止。当我在我的 VC 中使用

[[_downloadMapFileOperation signalForPauseDownloadMap]
    subscribeNext:^(id x) {
        [downloadMapView setTitleForStartDownloadButton:@"Resume download"];
        _currentDownloadState = kHTDownloadStatePaused;
    }];

我的进度值工作正常(不会从一个值跳到另一个值)。请帮帮我。谢谢。

4

1 回答 1

2

对于您关于跳跃下载进度的原始问题,问题是您正在创建对两个信号副本的订阅,这会导致两次下载。即,这段代码:

[[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place]
 subscribeNext:^(AFHTTPRequestOperation *operation) {
    NSString *alertMessage = @"Download complete";
    [[[UIAlertView alloc] initWithTitle:@"Complete" message:alertMessage delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
    downloadMapView.progressLabel.text = @"Download Complete";
}];

[[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place] subscribeError:^(NSError *error) {
    [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}];

每个subscribeNext/ subscribeError/等。将创建一个新订阅,并开始下载。您要用于订阅的内容如下:

[[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place]
 subscribeNext:^(AFHTTPRequestOperation *operation) {
    NSString *alertMessage = @"Download complete";
    [[[UIAlertView alloc] initWithTitle:@"Complete" message:alertMessage delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
    downloadMapView.progressLabel.text = @"Download Complete";
} error:^(NSError *error) {
    [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}];

对于暂停和恢复,我认为创建更多信号没有意义。如果您确实可以访问下载文件操作,则只需直接调用 pause/resume 即可。原始订阅将继续有效,并应根据创建信号中的行为暂停/恢复更新。

至于取消,您真正想做的是使用一次性用品。在[RACSignal createSignal:]通话中,您不想返回nil。相反,设置一个一次性的,当一次性的被丢弃时取消下载。

编辑

理想情况下,您希望通过信号发送进度。在setDownloadProgressBlock你会改为:

NSMutableDictionary *progressDictionary = [[NSMutableDictionary alloc] init];
progressDictionary[@"current"] = @(totalBytesRead);
progressDictionary[@"total"] = @(totalBytesExpectedToRead);
progressDictionary[@"progress"] = @((float)totalBytesRead / (float)totalBytesExpectedToRead);
[subscriber sendNext:[progressDictionary copy]];

然后,您可以在订阅时阅读 progressDictionary,如下所示:

[[[DownloadMapFileOperation sharedInstance] signalForDownloadMap:self.place]
subscribeNext:^(NSDictionary *progressDictionary) {
    // Do stuff with dictionary here, like update UI with progress
} error:^(NSError *error) {
    // Error related stuff
} completed:^{
    // Completed stuff
}];

注意:你不应该在没有充分理由的情况下通过 sendNext 发送 nil。完成逻辑应该只在完成块中完成。

于 2015-06-02T16:57:37.043 回答