gpt4 book ai didi

ios - ReactiveCocoa 和代表

转载 作者:行者123 更新时间:2023-11-28 19:01:02 25 4
gpt4 key购买 nike

我正在尝试与登录服务通信并以 react 方式更新 UI。问题是我的登录服务与委托(delegate)一起工作,而且我发现的几乎每个示例都与 block 一起工作。

我写了一个可行的解决方案,但它似乎有点笨拙,我不确定这是否是最好的方法:

登录 View Controller :

- (void) viewDidLoad
{
[super viewDidLoad];

//Assign the "loginCommand" command to the button. It'll get executed on button pressed and the button is only enabled when the command says so.
self.entrarBtn.rac_command = self.viewModel.loginCommand;

//Subscribe and respond to command's successful signals
@weakify(self);
[self.viewModel.loginCommand.executionSignals subscribeNext:^(RACSignal *loginSignal) {
[loginSignal subscribeNext:^(id x) {
@strongify(self);
[self.viewPresenter enterMainNavigation];
}];
}];

//Subscribe and respond to command's error signals
[self.viewModel.loginCommand.errors
subscribeNext:^(NSError* error) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[NSString stringWithFormat:@"Error: %@", error.localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}];
}

登录 View 模型:

- (id)init
{
self = [super init];

if(self) {
self.loginCommand = [[RACCommand alloc] initWithEnabled:self.enableLoginSignal
signalBlock:^RACSignal *(id input) {
return [self loginSignal];
}];
}

return self;
}


- (RACSignal *)loginSignal
{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

//LOGIN OK
RACDisposable* loginOKDisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginOK)
fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
PositionGlobalService *positionGlobalService = [PositionGlobalService sharedInstance];
positionGlobalService.delegate = self;
[positionGlobalService getPositionGlobal];
}];

//GETTING USER INFO DELEGATE THEN SEND THE COMPLETED SIGNAL
RACDisposable* positionOKDisposable = [[self rac_signalForSelector:@selector(positionGlobalServiceDidReceivePositionGlobal)
fromProtocol:@protocol(PositionGlobalServiceDelegate)] subscribeNext:^(id x) {
[subscriber sendNext:nil];
[subscriber sendCompleted];
}];
RACDisposable* positionErrorDisposable = [[self rac_signalForSelector:@selector(positionGlobalServiceDidReceivePositionGlobalError:)
fromProtocol:@protocol(PositionGlobalServiceDelegate)] subscribeNext:^(id x) {
NSError* error = [NSError errorWithDomain:LoginErrorDomain code:LoginErrorGettingUserInfo userInfo:nil];
[subscriber sendError:error];
}];

//ERRORS
RACDisposable* loginKODisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginKO)
fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
NSError* error = [NSError errorWithDomain:LoginErrorDomain code:LoginErrorKO userInfo:nil];
[subscriber sendError:error];
}];


RACDisposable* deniedDisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginKOAccessDenied)
fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
NSError* error = [NSError errorWithDomain:LoginErrorDomain code:LoginErrorAccessDenied userInfo:nil];
[subscriber sendError:error];
}];

RACDisposable* connectionErrorDisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveConnectionError)
fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
NSError* error = [NSError errorWithDomain:LoginErrorDomain code:LoginErrorConnectionError userInfo:nil];
[subscriber sendError:error];
}];

RACDisposable* genericErrorDisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveGenericError:)
fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
NSError* error = [NSError errorWithDomain:LoginErrorDomain code:LoginErrorGenericError userInfo:nil];
[subscriber sendError:error];
}];


LoginService *loginService = [LoginService sharedInstance];
loginService.delegate = self;
[loginService checkLogin:self.usuario withPassword:self.password documentType:LoginDocumentTypeNIF saveLogin:YES];


return [RACDisposable disposableWithBlock:^{
[loginOKDisposable dispose];
[positionOKDisposable dispose];
[positionErrorDisposable dispose];
[loginKODisposable dispose];
[deniedDisposable dispose];
[connectionErrorDisposable dispose];
[genericErrorDisposable dispose];
}];

}];
}

如您所见,有一堆代码几乎对每个委托(delegate)都相同,这就是为什么我不确定这是否是最好的方法。

最佳答案

你的观点看起来不错,但我对模型有一些建议。要点是我将简化 LoginServicePositionGlobalService 上的信号,方法是将它们移动到这些服务的相应类中。然后您可以合并错误并创建一个信号,例如:

@interface LoginService : SomeSuperclass<LoginServiceDelegate>
- (RACSignal *)loginWithID:(NSString *)userid password:(NSString *password);
@end

@implementation LoginService()

- (RACSignal *)loginWithID:(NSString *)userid password:(NSString *)password {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
RACDisposable *errorDisposable = [[RACSignal merge:@[[[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginKO) fromProtocol:@protocol(LoginServiceDelegate)] mapReplace:[NSError errorWithDomain:LoginErrorDomain code:LoginErrorKO userInfo:nil]],
[[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginKOAccessDenied) fromProtocol:@protocol(LoginServiceDelegate)] mapReplace:[NSError errorWithDomain:LoginErrorDomain code:LoginErrorAccessDenied userInfo:nil]],
[[self rac_signalForSelector:@selector(loginServiceDidReceiveConnectionError) fromProtocol:@protocol(LoginServiceDelegate)] mapReplace:[NSError errorWithDomain:LoginErrorDomain code:LoginErrorConnectionError userInfo:nil]],
[[self rac_signalForSelector:@selector(loginServiceDidReceiveGenericError) fromProtocol:@protocol(LoginServiceDelegate)] mapReplace:[NSError errorWithDomain:LoginErrorDomain code:LoginErrorGenericError userInfo:nil]]]] subscribeNext:^(id x) {
[subscriber sendError:x];
}];
RACDisposable *loginDisposable = [[self rac_signalForSelector:@selector(loginServiceDidReceiveLoginOK) fromProtocol:@protocol(LoginServiceDelegate)] subscribeNext:^(id x) {
[subscriber sendNext:x];
[subscriber sendCompleted];
}];

[self checkLogin:userid withPassword:password documentType:LoginDocumentTypeNIF saveLogin:YES];
return [RACDisposable disposableWithBlock:^{
[errorDisposable dispose];
[loginDisposable dispose];
}];
}
}
@end

然后,您的登录函数可以变成这样(尽管我可能会重命名此函数,因为它做了两件事):

- (RACSignal *)loginSignal
{
return [[[LoginService sharedInstance] loginWithID:self.usuario password:self.password] then:^RACSignal *{
return [[PositionGlobalService sharedInstance] getPositionGlobalSignal];
}];
}];

关于ios - ReactiveCocoa 和代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781567/

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