gpt4 book ai didi

ios - 带有核心数据的 MVVM 应用程序中 ReactiveCocoa 的可能竞争条件

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:44 26 4
gpt4 key购买 nike

我有一个演示应用程序,用于显示我正在开发的应用程序中可能存在的竞争条件。该应用程序显示 2 个 UITextField:textField1 和 textField2。

以下条件适用:

  • textField1 字符串应至少与 textField2 字符串一样长。
  • textField2 字符串不应超过 5 个字符。

该应用使用 MVVM 模式和 Reactive Cocoa。

应用应仅保存 textField1 和 textField2 的有效配置。

实体.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

最佳答案

竞争条件可能是 name2IsValidSignal 可以在 name1IsValidSignal 为假之前为真。这意味着 isValidSignal 在变为假之前可以暂时为真(这将触发保存)。一种解决方案是将验证逻辑合并到一个单元中:

    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 仅在所有验证逻辑运行后才更新。

关于ios - 带有核心数据的 MVVM 应用程序中 ReactiveCocoa 的可能竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com