我正在使用反应可可从服务器创建下载文件。我有一个带有进度属性(进度值、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;
}];
我的进度值工作正常(不会从一个值跳到另一个值)。请帮帮我。谢谢。