使用反应 cocoa 检查 nil 模型?
Checking for nil model using reactive cocoa?
我想知道使用反应式 cocoa 检查 nil 值的方法是什么。
我想我可以像这样创建一个信号。
// Turn state check into a signal to activate the rest
RACSignal* modelSignal = [self checkIfModelIsValid:self.model];
[[modelSignal then:^RACSignal *{
return [self obtainImageSignal];
// Always need to lazy call these functions
}]
subscribeNext:^(id x) {
NSLog(@"It worked out! giving you null for fun though feel free to chain it? %@",x);
}
error:^(NSError *error) {
// Replace with real ns error
NSLog(@"Model or picture is nil: %@",error);
}
completed:^{
NSLog(@"Model this event started it all");
}];
-(RACSignal *)obtainImageSignal {
@weakify(self)
RACSignal* imageSignal = [[NetworkManager sharedInstance] getImageWithImagePath:self.model.fullSizedPicture];
// Processes that image signal
[[imageSignal deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(UIImage* image) {
@strongify(self)
self.imageView.image = image;
[self setUpTitle:self.model.title];
[self setUpDescription:self.model.imageDescription];
}
error:^(NSError* error) {
NSLog(@"Error: %@",[error description]);
}
completed:^{
NSLog(@"Completed Operation: Image Retrieval Operation");
}];
return imageSignal;
}
-(RACSignal *) checkIfModelIsValid:(PhotoModel*) model {
// Using then on this signal will force the image signal to handle the errors.?
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
if (self.model == nil || self.model.fullSizedPicture == nil) {
[subscriber sendError:nil];
}
else {
[subscriber sendNext:[NSNumber numberWithBool:YES]];
[subscriber sendCompleted];
// send complete for fun and see what it does
}
return nil;
}];
}
或者完整性检查不适用于 React 函数式编程的情况?
只是为了给其他观众举个例子,一种确保您没有从 nil
模型中获取任何图像的方法 - 可以这样做:
RAC(self.someImageView, image) = [[RACObserve(self, model)
filter:^BOOL(PhotoModel *currentModel) {
return currentModel != nil;
}]
map:^id(PhotoModel *currentModel) {
return [self someMethodToDownloadImageFromModel:currentModel];
}];
请注意我是如何尝试避免在我的 RAC 块中使用 self
并避免注入在使用 subscribeNext:^
、then:^
等时出现的副作用。这样您的代码将变得更易于管理和更多 RAC。
我想知道使用反应式 cocoa 检查 nil 值的方法是什么。
我想我可以像这样创建一个信号。
// Turn state check into a signal to activate the rest
RACSignal* modelSignal = [self checkIfModelIsValid:self.model];
[[modelSignal then:^RACSignal *{
return [self obtainImageSignal];
// Always need to lazy call these functions
}]
subscribeNext:^(id x) {
NSLog(@"It worked out! giving you null for fun though feel free to chain it? %@",x);
}
error:^(NSError *error) {
// Replace with real ns error
NSLog(@"Model or picture is nil: %@",error);
}
completed:^{
NSLog(@"Model this event started it all");
}];
-(RACSignal *)obtainImageSignal {
@weakify(self)
RACSignal* imageSignal = [[NetworkManager sharedInstance] getImageWithImagePath:self.model.fullSizedPicture];
// Processes that image signal
[[imageSignal deliverOn:[RACScheduler mainThreadScheduler]]
subscribeNext:^(UIImage* image) {
@strongify(self)
self.imageView.image = image;
[self setUpTitle:self.model.title];
[self setUpDescription:self.model.imageDescription];
}
error:^(NSError* error) {
NSLog(@"Error: %@",[error description]);
}
completed:^{
NSLog(@"Completed Operation: Image Retrieval Operation");
}];
return imageSignal;
}
-(RACSignal *) checkIfModelIsValid:(PhotoModel*) model {
// Using then on this signal will force the image signal to handle the errors.?
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
if (self.model == nil || self.model.fullSizedPicture == nil) {
[subscriber sendError:nil];
}
else {
[subscriber sendNext:[NSNumber numberWithBool:YES]];
[subscriber sendCompleted];
// send complete for fun and see what it does
}
return nil;
}];
}
或者完整性检查不适用于 React 函数式编程的情况?
只是为了给其他观众举个例子,一种确保您没有从 nil
模型中获取任何图像的方法 - 可以这样做:
RAC(self.someImageView, image) = [[RACObserve(self, model)
filter:^BOOL(PhotoModel *currentModel) {
return currentModel != nil;
}]
map:^id(PhotoModel *currentModel) {
return [self someMethodToDownloadImageFromModel:currentModel];
}];
请注意我是如何尝试避免在我的 RAC 块中使用 self
并避免注入在使用 subscribeNext:^
、then:^
等时出现的副作用。这样您的代码将变得更易于管理和更多 RAC。