What is a correct alternative for subscribing to signals in ReactiveCocoa for network calls?

允我心安 提交于 2019-12-13 06:30:00

问题


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.

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];
        }

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"];
 }];

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?


回答1:


Network calls aren't special, you can compose signals that issue network requests the same way you would compose any other signal.



来源:https://stackoverflow.com/questions/24576833/what-is-a-correct-alternative-for-subscribing-to-signals-in-reactivecocoa-for-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!