GLD_WeiboListController.m
5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// GLD_WeiboListController.m
// YX_BaseProject
//
// Created by yiyangkeji on 2018/4/10.
// Copyright © 2018年 com.yxvzb. All rights reserved.
//
#import "GLD_WeiboListController.h"
#import "GLD_UserModel.h"
#import "RACDynamicSequence.h"
#import "GLD_SegmentView.h"
@interface GLD_WeiboListController ()
@property (nonatomic, strong)RACCommand *command;
@property (nonatomic, strong)UITextField *textfield;
@end
@implementation GLD_WeiboListController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
GLD_SegmentView *redv = [GLD_SegmentView new];
[self.view addSubview:redv];
redv.frame = CGRectMake(50, 40, 50, 40);
redv.backgroundColor = [UIColor redColor];
UIButton *btu = [UIButton new];
[redv addSubview:btu];
btu.frame = CGRectMake(10, 10, 30, 30);
[btu addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
_textfield = [UITextField new];
[self.view addSubview:_textfield];
_textfield.frame = CGRectMake(100, 100, 100, 100);
RAC(self,title) = RACObserve(self, textfield.text);
_textfield.borderStyle = UITextBorderStyleBezel;
//#pragma("clang diagnostic push")
//#pragma("clang diagnostic ignored "-Warc-performSelector-leaks"")
[self performSelector:@selector(multiRequest) withObject:nil];
//#pragma("clang diagnostic pop")
[self creatSignal];
}
//多个网络请求,统一处理
- (void)multiRequest{
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[subscriber sendNext:@1];
[subscriber sendCompleted];
});
return [RACDisposable disposableWithBlock:^{
NSLog(@"raquest1 disposable");
}];
}];
RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@2];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"request2 disposable");
}];
}];
[self rac_liftSelector:@selector(butClick:s2:) withSignalsFromArray:@[request1,request2]];
}
//统一处理网络请求
- (void)butClick:(id)data s2:(id)data2{
NSLog(@"data = %@\n data2 = %@\n",data,data2);
}
static NSInteger num = 0;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self sendSignal];
}
/**
- (void)commandSignalTest{
RACCommand *command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@(num++)];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
}];
}];
}];
//强引用,防止命令执行的时候释放命令
self.command = command;
[self.command.executionSignals.switchToLatest subscribeNext:^(id x) {
NSLog(@"%@\n",x);
}];
}
- (void)commandSubscribe{
// 5.执行命令
[self.command execute:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.command execute:@1];
});
}
*/
- (void)sendSignal{
if (self.delegateSubject) {
//订阅信号,即可收到信号发送的内容
[self.delegateSubject subscribeNext:^(id x) {
NSLog(@"-----%@",x);
}];
}
}
- (void)creatSignal{
//创建信号
self.delegateSubject = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//发送信号
[subscriber sendNext:@"dddd"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[subscriber sendNext:@"ccccc"];
});
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"++++++");
}];
}] doNext:^(id x) {
NSLog(@"doNext");
}];
}
- (void)RACchangeToArray{
NSDictionary *dict = @{@"name":@"1",@"age":@"2"};
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
[arr addObject:dict];
}
NSArray *arr1 = [[arr.rac_sequence map:^id(id value) {
return [GLD_UserModel yy_modelWithDictionary:value];
}] array];
//分步操作
//映射成一个动态的集合
RACDynamicSequence *ddd = [arr.rac_sequence map:^id(id value) {
return [GLD_UserModel yy_modelWithDictionary:value];
}];
NSArray *arr2 = ddd.array;
//arr1 和arr2 是数组内容相同
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc{
NSLog(@"%@-->死了\n",self.description);
}
@end