MVVM. RAC程序员iOS Developer

RACSubject 发送问题

2017-04-20  本文已影响97人  slimsallen

问题描述

需要在collectionview点击事件里面 调用网络请求在viewModel里获取详情数据。然后将详情数据发送给controller。

附上RACSubject用法

    RACSubject *subject = [RACSubject subject];
    [subject subscribeNext:^(id x) {
        NSLog(@"订阅者%@", x);
    }];
    [subject sendNext:@"subject"];

之前点击事件的代码是这样的

        self.viewModel.myID = model.article_id;
        [self.viewModel.servicePageCommand execute:nil];
        @weakify(self)
        [self.viewModel.htmlRefreshEndSubject subscribeNext:^(id x) {
            
            @strongify(self)
            NSDictionary *dict = @{@"titleContent":model.name,
                                   
                                   @"linkAdress":self.viewModel.htmldataArray[0],
                                   @"type":model.channel_type
                                   };
            NSString * content = [dict mj_JSONString];
            [self.viewModel.cellClickSubject sendNext:content];
        }];

最后发现 controller会跳多次,第一次正常,第二次跳两次 依次累加。

最后经过排查发现这边的[self.viewModel.cellClickSubject sendNext:content];会也走多次。
不知道为什么。

于是还是像之前一样写在了bindviewmodel里 专门去订阅信号,然后发现发送正常。

但是不知道 为什么写在collectionview点击事件里 就会累计发送,是会累计上次的信号吗?求解?

上一篇 下一篇

猜你喜欢

热点阅读