我有一个演示应用程序,用于在我正在处理的应用程序中显示可能的竞争条件。该应用程序显示2 UITextField:textField1和textField2。
以下条件适用:
该应用程序使用MVVM模式和Reactive Cocoa。
应用程序应仅保存textField1和textField2的有效配置。
Entity.h
@interface Entity : NSManagedObject
@property (nonatomic, retain) NSString * name1;
@property (nonatomic, retain) NSString * name2;
@end
ViewController.h
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "ViewModel.h"
@interface ViewController ()
@property(nonatomic, weak) IBOutlet UITextField *textField1;
@property(nonatomic, weak) IBOutlet UITextField *textField2;
@property(nonatomic, strong) ViewModel *viewModel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.viewModel = [[ViewModel alloc] init];
// Two-way binding between textfield1 and self.viewModel.name1
// We cannot use a simple RACChannelTo = RACChannelTo because Textfield.text doesn't fire KVO notification on each strokes but only when exit the textfield.
RACChannelTerminal *name1ModelTerminal = RACChannelTo(self, viewModel.name1);
RAC(self.textField1, text) = name1ModelTerminal;
[self.textField1.rac_textSignal subscribe:name1ModelTerminal];
// Two-way binding between textfield2 and self.viewModel.name2
// We cannot use a simple RACChannelTo = RACChannelTo because Textfield.text doesn't fire KVO notification on each strokes but only when exit the textfield.
RACChannelTerminal *name2ModelTerminal = RACChannelTo(self, viewModel.name2);
RAC(self.textField2, text) = name2ModelTerminal;
[self.textField2.rac_textSignal subscribe:name2ModelTerminal];
// Validation
RAC(self.textField1, backgroundColor) = [self.viewModel.name1IsValidSignal map:^id(NSNumber *value) {
if ([value boolValue]) {
return [UIColor clearColor];
} else {
return [UIColor redColor];
}
}];
RAC(self.textField2, backgroundColor) = [self.viewModel.name2IsValidSignal map:^id(NSNumber *value) {
if ([value boolValue]) {
return [UIColor clearColor];
} else {
return [UIColor redColor];
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ViewModel.h
#import "RVMViewModel.h"
@interface ViewModel : RVMViewModel
@property(nonatomic, strong) NSString *name1;
@property(nonatomic, strong) NSString *name2;
@property(nonatomic, strong) RACSignal *name1IsValidSignal;
@property(nonatomic, strong) RACSignal *name2IsValidSignal;
@property(nonatomic, strong) RACSignal *isValidSignal;
@end
ViewModel.m
#import "ViewModel.h"
#import "Entity.h"
@interface ViewModel ()
@property(nonatomic, strong) Entity *model;
@property(nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
@implementation ViewModel
-(instancetype)init {
self = [super init];
if (self) {
[self initialize];
}
return self;
}
-(void)initialize {
// setup Model
self.managedObjectContext = [NSManagedObjectContext MR_defaultContext];
self.model = [Entity MR_createInContext:self.managedObjectContext];
// log
[RACObserve(self, name1) subscribeNext:^(NSString *name) {
NSLog(@"viewModel.name1 = %@", name);
}];
[RACObserve(self, name2) subscribeNext:^(NSString *name) {
NSLog(@"viewModel.name2 = %@", name);
}];
[RACObserve(self, model.name1) subscribeNext:^(NSString *name) {
NSLog(@"model.name1 = %@", name);
}];
[RACObserve(self, model.name2) subscribeNext:^(NSString *name) {
NSLog(@"model.name2 = %@", name);
}];
// Validation
@weakify(self);
self.name1IsValidSignal = [RACSignal combineLatest:@[RACObserve(self, name1),
RACObserve(self, name2)]
reduce:^id(NSString *name1, NSString *name2) {
return @([name1 length] >= [name2 length]);
}];
self.name2IsValidSignal = [RACObserve(self, name2) map:^id(NSString *value) {
return @([value length] < 6);
}];
self.isValidSignal = [[RACSignal combineLatest:@[self.name1IsValidSignal, self.name2IsValidSignal]] and];
// Initial value of Model -> ViewModel
self.name1 = self.model.name1;
self.name2 = self.model.name2;
// Binding: ViewModel -> Model when name is valid
RAC(self, model.name1) = [[self.name1IsValidSignal filter:^BOOL(id value) {
return [value boolValue];
}] map:^id(id value) {
@strongify(self);
return self.name1;
}];
RAC(self, model.name2) = [[self.name2IsValidSignal filter:^BOOL(id value) {
return [value boolValue];
}] map:^id(id value) {
@strongify(self);
return self.name2;
}];
// Save with Core Data when both name1IsValidSignal and name2IsValidSignal returns @YES and that there are some unsaved changes in the context
// There is a possible race condition here when this subscription is called before the bindings from ViewModel -> Model is called.
[[[RACSignal combineLatest:@[RACObserve(self.managedObjectContext, hasChanges), self.isValidSignal]] and] subscribeNext:^(id x) {
if ([x boolValue]) {
// Save with Core Data
[self.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
// TODO: error management
if (success || !error) {
NSLog(@"Success");
NSLog(@" ");
} else {
NSLog(@"Problem when saving to Core Data");
}
}];
}
}];
}
@end
从我的角度来看,ViewModel.m中最后一个订阅中的Core Data可能会在模型更新之前发生。你如何防止这种情况发生?
修改 我真正想要的是推送只通过验证的(name1和name2)配置的Core Data模型。
Xcode项目 https://github.com/guillaume-michel/ReactiveValidation
答案 0 :(得分:1)
竞争条件可能是name2IsValidSignal在name1IsValidSignal为false之前为true。这意味着isValidSignal在变为false之前可以是真实的(这会触发保存)。一种解决方案是将验证逻辑组合到一个单元中:
self.isValidSignal = [RACSignal combineLatest:@[RACObserve(self, name1),
RACObserve(self, name2)]
reduce:^id(NSString *name1, NSString *name2) {
return @([name1 length] >= [name2 length] && [name2 length] < 6);
}];
这应该确保只有在运行所有验证逻辑后才更新isValidSignal。
答案 1 :(得分:1)
根据docs for -[NSManagedObjectContext hasChanges]
,您不应该在收到KVO通知的同时向NSManagedObjectContext发送消息:
如果由于更改hasChanges的值而需要向上下文发送消息或更改其任何托管对象,则必须在调用堆栈展开后执行此操作(通常使用performSelector:withObject:afterDelay:或类似的方法)。
您可以通过明确传递到+[RACScheduler mainThreadScheduler]
:
[[[[RACSignal combineLatest:@[RACObserve(self.managedObjectContext, hasChanges),
self.isValidSignal]]
and]
deliverOn:RACScheduler.mainThreadScheduler]
subscribeNext:^(id x) {
// ...