订阅ReactiveCocoa中的信号以进行网络呼叫的正确选择是什么? [英] What is a correct alternative for subscribing to signals in ReactiveCocoa for network calls?
问题描述
我是 ReactiveCocoa
的新手,并且在阅读了ReactiveCocoa的最佳做法之后这里我知道我需要避免显式订阅和处置
但是在所有关于网络的教程和 ReactiveCocoa
中我看到了相同的模式:创建信号(make GET
或 POST
请求服务器,解析结果, sendNext
, sendCompleted
) - > subcsribeNext
(做UI内容或其他结果) - > subscribeError
。因此,我们认为这里有一个明确的订阅,这是不好的。
是否有更正确和概念上纯粹的方法来做这个常见的事情? rac_liftSelector:withSignals:
或类似的东西?或者当我们处理网络电话和 AFNetworking
时,我们应该始终使用这种标准订阅模式吗?详细解释将非常有用。
I'm new to ReactiveCocoa
world and after reading best practices of ReactiveCocoa here I knew that I need to "avoid explicit subscriptions and disposal"
but in all tutorials about network and ReactiveCocoa
I saw the same pattern : create signal (make GET
or POST
request to server, parse result, sendNext
, sendCompleted
) -> subcsribeNext
(do UI stuff or something other with the result) -> subscribeError
. So as we see there is an explicit subscription here, which is not good, I think.
Are there some more correct and conceptually pure ways of doing this common thing? rac_liftSelector:withSignals:
or something like this? Or when we deal with network calls and AFNetworking
we should always use this standard subscription pattern? Detailed explanation will be very helpful.
编辑:
在我的应用程序中,我主要是取电话,其中一些是是依赖的,其他人是单身(绝大多数),如登录
或 fetchWhatever
,或 postWhatever
。我使用相同的模式构建的所有API调用( self
- 是我的 API管理器
NVMAPI
类 AFHTTPSessionManager
子类):
EDITS:
In my application I have mainly fetching calls, some of them are dependent and others are single (vast majority) like login
or fetchWhatever
, or postWhatever
. All API calls I construct with the same pattern like this (self
- is my API manager
NVMAPI
class which is AFHTTPSessionManager
subclass):
-(RACSignal*)loginUserWithEmail:(NSString *)email andPassword:(NSString *)password
{
__block NSURLSessionDataTask* task;
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
task = [self GET:kUserLoginEndpoint
parameters:@{@"email": email, @"password": password}
success:^(NSURLSessionDataTask *task, id responseObject) {
NVMUser* user = [[NVMUser alloc] initWithDictionary:responseObject[@"user"]];
[subscriber sendNext:user];
[subscriber sendCompleted];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[subscriber sendError:error];
}];
return [RACDisposable disposableWithBlock:^{
[task cancel];
}];
}] replayLazily];
}
我不使用 MVVM
并使用简单的MVC。这里我如何在视图控制器中进行API调用和创建信号:
I don't use MVVM
and use simple MVC. Here how I make API calls and create signals in view controllers:
[SVProgressHUD showWithStatus:@"Processing..." maskType:SVProgressHUDMaskTypeBlack];
[[[NVMAPI api] loginUserWithEmail:self.emailTextField.text
andPassword:self.passwordTextField.text]
subscribeNext:^(id x) {
[self.activeUser setupWithUser:x];
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:kLoginSeque sender:self];
}
error:^(NSError *error) {
[SVProgressHUD dismiss];
[self showAlertWithText:error.localizedDescription title:@"Error"];
}];
我使用的所有信号都是这样的: fetchComments
, postStatus
等。如果我有从属调用,我使用 flattenMap
。所以我很感兴趣 - 它是一种创建信号和使用它们的正确方法(简单的 subscribeNext
)?或者这可以通过一些更正确和优雅的方式来实现?
编辑2:
我看到订阅的主要问题 - 我不知道如何使用它们实现tableview的分页。我有 fetchComment
方法,带有加载分页注释 - 每个页面包含15条注释。我不能同时使用 subcribeNext
或 RAC()
绑定,对吗?我该如何管理这种模式?
All signals I use the same way like: fetchComments
, postStatus
, etc. If I have dependent calls I use flattenMap
. So I'm interested - is it a right approach for creating signals and for using them (simple subscribeNext
)? Or this can be achieved with some more correct and elegant way?
EDIT 2:
Main problem with subscriptions which I see - I don't know how to implement pagination for tableview with them. I have fetchComment
method with load paginated comments - each page contain 15 comments. I can't use neither subcribeNext
nor RAC()
binding for this, right?How can I manage this pattern?
推荐答案
网络呼叫并不特殊,您可以像编写任何其他信号一样编写发出网络请求的信号。
Network calls aren't special, you can compose signals that issue network requests the same way you would compose any other signal.
这篇关于订阅ReactiveCocoa中的信号以进行网络呼叫的正确选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!