Home ReactiveCocoa (6) 多次订阅一次请求
Post
Cancel

ReactiveCocoa (6) 多次订阅一次请求

每次订阅时都会发送请求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//1 创建信号
RACSignal * signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    NSLog(@"发送数据的请求");
    //3 发送请求
    [subscriber sendNext:@1];
    
    return nil;
}];

//2 订阅信号
[signal subscribeNext:^(id x) {
    NSLog(@"订阅者一 %@",x);
}];

[signal subscribeNext:^(id x) {
    NSLog(@"订阅者二 %@",x);
}];

测试结果:

如何做到多次订阅一次请求呢?有下面两个方法:

1、RACSubject 多次订阅 一起请求

1
2
3
4
5
6
7
8
9
10
11
RACSubject * subject = [RACSubject subject];

[subject subscribeNext:^(id x) {
    NSLog(@"1 %@",x);
}];

[subject subscribeNext:^(id x) {
    NSLog(@"2 %@",x);
}];
//一次请求
[subject sendNext:@1];

运行测试:

2、RACMulticastConnection 把信号转换为连接类

不管订阅多少次,只会请求一次,连接类必须有信号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//1 创建信号
RACSignal * signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    
    NSLog(@"发送数据的请求");
    
    [subscriber sendNext:@"发送的数据"];
    return nil;
}];

//2 把信号转换成连接类
RACMulticastConnection * cnt = [signal multicast:[RACSubject subject]];

//3 订阅连接类信号
[cnt.signal subscribeNext:^(id x) {
    NSLog(@"订阅者:%@",x);
}];

[cnt.signal subscribeNext:^(id x) {
    NSLog(@"订阅者二 %@",x);
}];

//4 连接
[cnt connect];

测试结果:

This post is licensed under CC BY 4.0 by the author.

ReactiveCocoa (2)信号的订阅、取消订阅、接收

ReactiveCocoa (4)常用方法集