-
Notifications
You must be signed in to change notification settings - Fork 0
/
qian-tan-rxjava.html
237 lines (176 loc) · 13.6 KB
/
qian-tan-rxjava.html
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>codemixwine</title>
<meta name="description" content="">
<meta name="author" content="slock">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://www.codemixwine.com/theme/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="http://www.codemixwine.com/theme/bootstrap.min.css" rel="stylesheet">
<link href="http://www.codemixwine.com/theme/bootstrap.min.responsive.css" rel="stylesheet">
<link href="http://www.codemixwine.com/theme/local.css" rel="stylesheet">
<link href="http://www.codemixwine.com/theme/pygments.css" rel="stylesheet">
<!-- So Firefox can bookmark->"abo this site" -->
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://www.codemixwine.com">codemixwine</a>
<div class="nav-collapse">
<ul class="nav">
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<div class="content">
<div class="row">
<div class="span9">
<div class='article'>
<div class="content-title">
<h1>浅谈Rxjava</h1>
2015-12-14
by <a class="url fn" href="http://www.codemixwine.com/author/slock.html">Slock</a>
</div>
<div><p><strong>RxJava</strong>正在Android开发者中变的越来越流行。唯一的问题就是上手不容易,尤其是大部分人之前都是使用命令式编程语言。但是一旦你弄明白了,你就会发现RxJava真是太棒了。
这里仅仅是帮助你了解RxJava,整个系列共有四篇文章,希望你看完这四篇文章之后能够了解RxJava背后的思想,并且喜欢上RxJava。</p>
<h2>基础</h2>
<p>RxJava最核心的两个东西是Observables(被观察者,事件源)和Subscribers(观察者)。Observables发出一系列事件,Subscribers处理这些事件。这里的事件可以是任何你感兴趣的东西(触摸事件,web接口调用返回的数据。。。)</p>
<p>一个Observable可以发出零个或者多个事件,知道结束或者出错。每发出一个事件,就会调用它的Subscriber的onNext方法,最后调用Subscriber.onNext()或者Subscriber.onError()结束。</p>
<p>Rxjava的看起来很想设计模式中的观察者模式,但是有一点明显不同,那就是如果一个Observerble没有任何的的Subscriber,那么这个Observable是不会发出任何事件的。</p>
<h2>Hello World</h2>
<p>创建一个Observable对象很简单,直接调用Observable.create即可</p>
<div class="highlight"><pre><span class="nx">Observable</span><span class="o"><</span><span class="nb">String</span><span class="o">></span> <span class="nx">myObservable</span> <span class="o">=</span> <span class="nx">Observable</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span>
<span class="k">new</span> <span class="nx">Observable</span><span class="p">.</span><span class="nx">OnSubscribe</span><span class="o"><</span><span class="nb">String</span><span class="o">></span><span class="p">()</span> <span class="p">{</span>
<span class="err">@</span><span class="nx">Override</span>
<span class="kr">public</span> <span class="k">void</span> <span class="nx">call</span><span class="p">(</span><span class="nx">Subscriber</span><span class="cp"><?</span> <span class="nx">super</span> <span class="nx">String</span><span class="o">></span> <span class="nx">sub</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">sub</span><span class="o">.</span><span class="nx">onNext</span><span class="p">(</span><span class="s2">"Hello, world!"</span><span class="p">);</span>
<span class="nx">sub</span><span class="o">.</span><span class="nx">onCompleted</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div>
<p>这里定义的Observable对象仅仅发出一个Hello World字符串,然后就结束了。接着我们创建一个Subscriber来处理Observable对象发出的字符串。</p>
<div class="highlight"><pre><span class="nx">Subscriber</span><span class="o"><</span><span class="kt">String</span><span class="o">></span> <span class="n">mySubscriber</span> <span class="o">=</span> <span class="nb">new</span> <span class="nx">Subscriber</span><span class="o"><</span><span class="kt">String</span><span class="o">></span><span class="p">()</span> <span class="p">{</span>
<span class="p">@</span><span class="nx">Override</span>
<span class="k">public</span> <span class="nf">void</span> <span class="nx">onNext</span><span class="p">(</span><span class="kt">String</span> <span class="nb">s</span><span class="p">)</span> <span class="p">{</span> <span class="nx">System.out.println</span><span class="p">(</span><span class="nb">s</span><span class="p">);</span> <span class="p">}</span>
<span class="p">@</span><span class="nx">Override</span>
<span class="k">public</span> <span class="nf">void</span> <span class="nx">onCompleted</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span>
<span class="p">@</span><span class="nx">Override</span>
<span class="k">public</span> <span class="nf">void</span> <span class="nx">onError</span><span class="p">(</span><span class="nx">Throwable</span> <span class="nx">e</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>这里subscriber仅仅就是打印observable发出的字符串。通过subscribe函数就可以将我们定义的myObservable对象和mySubscriber对象关联起来,这样就完成了subscriber对observable的订阅。</p>
<div class="highlight"><pre><span class="n">myObservable</span><span class="p">.</span><span class="n">subscribe</span><span class="p">(</span><span class="n">mySubscriber</span><span class="p">);</span>
</pre></div>
<p>一旦mySubscriber订阅了myObservable,myObservable就是调用mySubscriber对象的onNext和onComplete方法,mySubscriber就会打印出Hello World!</p>
<h2>更简洁的代码</h2>
<p>是不是觉得仅仅为了打印一个hello world要写这么多代码太啰嗦?我这里主要是为了展示RxJava背后的原理而采用了这种比较啰嗦的写法,RxJava其实提供了很多便捷的函数来帮助我们减少代码。</p>
<p>首先来看看如何简化Observable对象的创建过程。RxJava内置了很多简化创建Observable对象的函数,比如Observable.just就是用来创建只发出一个事件就结束的Observable对象,上面创建Observable对象的代码可以简化为一行</p>
<div class="highlight"><pre><span class="nx">Observable</span><span class="o"><</span><span class="kt">String</span><span class="o">></span> <span class="n">myObservable</span> <span class="o">=</span> <span class="nx">Observable.just</span><span class="p">(</span><span class="s2">"Hello, world!"</span><span class="p">);</span>
</pre></div>
<p>接下来看看如何简化Subscriber,上面的例子中,我们其实并不关心OnComplete和OnError,我们只需要在onNext的时候做一些处理,这时候就可以使用Action1类。</p>
<div class="highlight"><pre><span class="nx">Action1</span><span class="o"><</span><span class="kt">String</span><span class="o">></span> <span class="n">onNextAction</span> <span class="o">=</span> <span class="nb">new</span> <span class="nx">Action1</span><span class="o"><</span><span class="kt">String</span><span class="o">></span><span class="p">()</span> <span class="p">{</span>
<span class="p">@</span><span class="nx">Override</span>
<span class="k">public</span> <span class="nf">void</span> <span class="nb">call</span><span class="p">(</span><span class="kt">String</span> <span class="nb">s</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">System.out.println</span><span class="p">(</span><span class="nb">s</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>subscribe方法有一个重载版本,接受三个Action1类型的参数,分别对应OnNext,OnComplete, OnError函数。</p>
<div class="highlight"><pre><span class="n">myObservable</span><span class="p">.</span><span class="n">subscribe</span><span class="p">(</span><span class="n">onNextAction</span><span class="p">,</span> <span class="n">onErrorAction</span><span class="p">,</span> <span class="n">onCompleteAction</span><span class="p">);</span>
</pre></div>
<p>这里我们并不关心onError和onComplete,所以只需要第一个参数就可以</p>
<div class="highlight"><pre><span class="n">myObservable</span><span class="p">.</span><span class="n">subscribe</span><span class="p">(</span><span class="n">onNextAction</span><span class="p">);</span>
<span class="c1">//Outputs "Hello, world!"</span>
</pre></div>
<p>上面的代码最终可以写成这样</p>
<div class="highlight"><pre><span class="nx">Observable.just</span><span class="p">(</span><span class="s2">"Hello, world!"</span><span class="p">)</span>
<span class="bp">.</span><span class="nx">subscribe</span><span class="p">(</span><span class="nb">new</span> <span class="nx">Action1</span><span class="o"><</span><span class="kt">String</span><span class="o">></span><span class="p">()</span> <span class="p">{</span>
<span class="p">@</span><span class="nx">Override</span>
<span class="k">public</span> <span class="nf">void</span> <span class="nb">call</span><span class="p">(</span><span class="kt">String</span> <span class="nb">s</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">System.out.println</span><span class="p">(</span><span class="nb">s</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
</pre></div>
<p>使用java8的lambda可以使代码更简洁</p>
<div class="highlight"><pre><span class="n">Observable</span><span class="p">.</span><span class="n">just</span><span class="p">(</span><span class="s">"Hello, world!"</span><span class="p">)</span>
<span class="p">.</span><span class="n">subscribe</span><span class="p">(</span><span class="n">s</span> <span class="o">-></span> <span class="n">System</span><span class="p">.</span><span class="n">out</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">));</span>
</pre></div>
<p>Android开发中,强烈推荐使用retrolambda这个gradle插件,这样你就可以在你的代码中使用lambda了。</p></div>
<hr>
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'codemixwine';
var disqus_title = '浅谈Rxjava';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</div>
<div class="span3">
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
Site
</li>
<li><a href="http://www.codemixwine.com/archives.html">Archives</a>
<li><a href="http://www.codemixwine.com/tags.html">Tags</a>
</ul>
</div>
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
Categories
</li>
<li><a href="http://www.codemixwine.com/category/android.html">Android</a></li>
<li><a href="http://www.codemixwine.com/category/sheng-huo.html">生活</a></li>
</ul>
</div>
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
Links
</li>
<li><a href="http://getpelican.com/">Pelican</a></li>
<li><a href="http://python.org/">Python.org</a></li>
<li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
</ul>
</div>
<div class="social">
<div class="well" style="padding: 8px 0; background-color: #FBFBFB;">
<ul class="nav nav-list">
<li class="nav-header">
Social
</li>
<li><a href="https://github.com/Slock">Github</a></li>
</ul>
</div>
</div>
</div>
</div> </div>
<footer>
<br />
<p><a href="http://www.codemixwine.com">codemixwine</a> © slock 2016</p>
</footer>
</div> <!-- /container -->
<script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js"></script>
<script src="http://www.codemixwine.com/theme/bootstrap-collapse.js"></script>
</body>
</html>