聽說如今流行的開發模式是 Retrofit+RxJava+MVP+ButterKnifephp
若是想要簡單學習ButterKnife、MVP模式,能夠參考我之前的例子
使用butterknife註解框架
Android—MVP設計模式高級(三)java
今天我就簡單來學習下RxJava的相關知識
之前我也只是據說過RxJava,RxJava這個究竟是什麼東西呢?
呵呵,它實際上是一個庫,因此咱們使用裏面的方法,得須要下載庫,因此咱們須要在AS中進行配置react
1.RxJava 地址以及添加android
github地址:
https://github.com/ReactiveX/RxJava
或者
https://github.com/ReactiveX/RxAndroidgit
依賴庫添加:
compile ‘io.reactivex:rxjava:1.1.6’
或者
compile ‘io.reactivex:rxandroid:1.2.1’github
2.RxJava是什麼類型的庫?它的原理是什麼?數據庫
RxJava 在 GitHub 主頁上的自我介紹是 「a library for composing asynchronous and event-based programs using observable sequences for the Java VM」(一個在 Java VM 上使用可觀測的序列來組成異步的、基於事件的程序的庫)。這就是 RxJava ,歸納得很是精準。編程
Rx:函數響應式編程,也許這個詞對你我都很只可意會,不可言傳,先拋開Rx不說,咱們接觸到的相似的這樣的思路,大概有接口回調、Handler通信、廣播通信、還有一個開源的EventBus、以及ContentPorivider裏面的觀察者模式、AsyncTask 咱們朦朧中也許就大概瞭解RxJava是怎麼個東西了。設計模式
RxJava 的異步實現,是經過一種擴展的觀察者模式來實現的。
至於觀察者我就拿ContentProvider來講吧,好比咱們在一個ContentProvider中有一個insert方法,插入完畢後,去通知某個監聽該URI變化的界面api
<code class="hljs avrasm has-numbering">getContext()<span class="hljs-preprocessor">.getContentResolver</span>()<span class="hljs-preprocessor">.notifyChange</span>(URI, null)<span class="hljs-comment">;</span></code><ul style="" class="pre-numbering"><li>1</li></ul><ul style="" class="pre-numbering"><li>1</li></ul>
好比在MainActivity中去registerContentObserver註冊一個內容觀察者
<code class="hljs java has-numbering"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Uri URI = Uri .parse(<span class="hljs-string">"content://com.example.contentprovider.MyContentProvider/student"</span>); contentResolver.registerContentObserver(uri, <span class="hljs-keyword">true</span>, observer);</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>
<code class="hljs java has-numbering"> <span class="hljs-keyword">private</span> ContentObserver observer = <span class="hljs-keyword">new</span> ContentObserver(<span class="hljs-keyword">null</span>) { <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onChange</span>(<span class="hljs-keyword">boolean</span> selfChange) { <span class="hljs-comment">// 說明數據有改變,從新查詢一直全部記錄 </span> Uri uri = Uri.parse(<span class="hljs-string">"content://com.example.contentprovider.MyContentProvider/student"</span>); Cursor cursor = contentResolver.query(uri, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>); Log.e(<span class="hljs-string">"TAG"</span>, <span class="hljs-string">"onChange() count="</span> + cursor.getCount()); }; }; </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>
那麼對於RxJava 的觀察者模式呢?
RxJava 有四個基本概念:Observable (被觀察者)、 Observer (觀察者)、 subscribe (訂閱)、事件。
對比ContentProvider,RxJava裏面的被觀察者就是某個Uri(也就是某個數據庫),觀察者就是某個界面(好比MainActivity),訂閱就是registerContentObserver,事件就是insert方法
Rxjava本質主要就是異步任務 外層構建了一個觀察者的設計模式 它更簡潔 咱們調用api方法 幾乎看不到 方法裏面的 分線程數據操做 它只是利用了 一種觀察者的設計模式 來進行 主分線程的通信 來進行響應操做
咱們來看看RxJava的察者模式流程交互圖:
與傳統觀察者模式不一樣, RxJava 的事件回調方法除了普通事件 onNext() (至關於 insert)以外,還定義了兩個特殊的事件:onCompleted() 和 onError()。
咱們來看下Observable是如何定義的?
<code class="hljs avrasm has-numbering"> Observable<span class="hljs-preprocessor">.create</span>(new Observable<span class="hljs-preprocessor">.OnSubscribe</span><String>() { @Override public void <span class="hljs-keyword">call</span>(Subscriber<? super String> subscriber) { Log<span class="hljs-preprocessor">.d</span>(TAG, <span class="hljs-string">"call: threadId:"</span> + Thread<span class="hljs-preprocessor">.currentThread</span>()<span class="hljs-preprocessor">.getId</span>())<span class="hljs-comment">;</span> subscriber<span class="hljs-preprocessor">.onStart</span>()<span class="hljs-comment">;</span> subscriber<span class="hljs-preprocessor">.onNext</span>(<span class="hljs-string">"Hello World!"</span>)<span class="hljs-comment">;</span> subscriber<span class="hljs-preprocessor">.onCompleted</span>()<span class="hljs-comment">;</span> } })</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
咱們看看create源碼裏面作了什麼?
<code class="hljs cs has-numbering"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <T> Observable<T> <span class="hljs-title">create</span>(OnSubscribe<T> f) { <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Observable<T>(hook.onCreate(f)); }</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>
能夠看到,這裏傳入了一個 OnSubscribe 對象做爲參數。OnSubscribe 會被存儲在返回的 Observable 對象中,當 Observable 被訂閱的時候,OnSubscribe 的 call() 方法會自動被調用,事件序列就會依照設定依次觸發(對於上面的代碼,就是觀察者Subscriber 將會被調用一次 onNext() 和一次 onCompleted())。這樣,由被觀察者調用了觀察者的回調方法,就實現了由被觀察者向觀察者的事件傳遞,即觀察者模式。
而後在看Observer中作了什麼操做?
<code class="hljs lasso has-numbering">subscribe(<span class="hljs-literal">new</span> Observer<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onCompleted() { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onCompleted: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onError(Throwable e) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>e(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onError: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onNext(<span class="hljs-built_in">String</span> s) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onNext: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onNext: s = "</span> <span class="hljs-subst">+</span> s); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li></ul>
onCompleted(): 事件隊列完結。RxJava 不只把每一個事件單獨處理,還會把它們看作一個隊列。RxJava 規定,當不會再有新的 onNext() 發出時,須要觸發 onCompleted() 方法做爲標誌。
onError(): 事件隊列異常。在事件處理過程當中出異常時,onError() 會被觸發,同時隊列自動終止,不容許再有事件發出。
在一個正確運行的事件序列中, onCompleted() 和 onError() 有且只有一個,而且是事件序列中的最後一個。須要注意的是,onCompleted() 和 onError() 兩者也是互斥的,即在隊列中調用了其中一個,就不該該再調用另外一個。
接下來咱們將所有的代碼貼出來,看看Log日誌打印:
同步方式
<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.example.administrator.myapplication; <span class="hljs-keyword">import</span> android.app.Activity; <span class="hljs-keyword">import</span> android.os.Bundle; <span class="hljs-keyword">import</span> android.util.Log; <span class="hljs-keyword">import</span> rx.Observable; <span class="hljs-keyword">import</span> rx.Observer; <span class="hljs-keyword">import</span> rx.Subscriber; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span> <span class="hljs-keyword">private</span> String TAG = <span class="hljs-string">"MainActivity"</span>; <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Observable.create(<span class="hljs-keyword">new</span> Observable.OnSubscribe<String>() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">call</span>(Subscriber<? <span class="hljs-keyword">super</span> String> subscriber) { Log.d(TAG, <span class="hljs-string">"call: threadId:"</span> + Thread.currentThread().getId()); subscriber.onStart(); subscriber.onNext(<span class="hljs-string">"Hello World!"</span>); subscriber.onCompleted(); } }) .subscribe(<span class="hljs-keyword">new</span> Observer<String>() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCompleted</span>() { Log.d(TAG, <span class="hljs-string">"onCompleted: threadId:"</span> + Thread.currentThread().getId()); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span>(Throwable e) { Log.e(TAG, <span class="hljs-string">"onError: threadId:"</span> + Thread.currentThread().getId()); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onNext</span>(String s) { Log.d(TAG, <span class="hljs-string">"onNext: threadId:"</span> + Thread.currentThread().getId()); Log.i(TAG, <span class="hljs-string">"onNext: s = "</span> + s); } }); } } </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li></ul>
<code class="hljs http has-numbering"><span class="hljs-attribute">call</span>: <span class="hljs-string">threadId:1</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">threadId:1</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">s = Hello World!</span> <span class="hljs-attribute">onCompleted</span>: <span class="hljs-string">threadId:1</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
從上能夠看出,事件的處理和結果的接收都是在同一個線程裏面處理的。可是,Rxjava的意義何在,異步呢?別急,看如下代碼的處理,你就會發現了,異步原來是這麼的簡單。
異步方式
咱們將上面的代碼稍微改下,增長2行代碼
<code class="hljs lasso has-numbering"> Observable<span class="hljs-built_in">.</span>create(<span class="hljs-literal">new</span> Observable<span class="hljs-built_in">.</span>OnSubscribe<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> call(Subscriber<span class="hljs-subst"><?</span> super <span class="hljs-built_in">String</span><span class="hljs-subst">></span> subscriber) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); subscriber<span class="hljs-built_in">.</span>onStart(); subscriber<span class="hljs-built_in">.</span>onNext(<span class="hljs-string">"Hello World!"</span>); subscriber<span class="hljs-built_in">.</span>onCompleted(); } }) <span class="hljs-built_in">.</span>subscribeOn(Schedulers<span class="hljs-built_in">.</span>io()) <span class="hljs-built_in">.</span>observeOn(AndroidSchedulers<span class="hljs-built_in">.</span>mainThread()) <span class="hljs-built_in">.</span>subscribe(<span class="hljs-literal">new</span> Observer<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onCompleted() { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onCompleted: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onError(Throwable e) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>e(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onError: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onNext(<span class="hljs-built_in">String</span> s) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onNext: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onNext: s = "</span> <span class="hljs-subst">+</span> s); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul>
咱們添上以下2行代碼
<code class="hljs avrasm has-numbering"><span class="hljs-preprocessor">.subscribeOn</span>(Schedulers<span class="hljs-preprocessor">.io</span>()) <span class="hljs-preprocessor">.observeOn</span>(AndroidSchedulers<span class="hljs-preprocessor">.mainThread</span>())</code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>
咱們看下Log輸出
<code class="hljs mathematica has-numbering"><span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">07</span>:<span class="hljs-number">20</span>:<span class="hljs-number">18.101</span> <span class="hljs-number">22734</span>-<span class="hljs-number">22734</span>/? <span class="hljs-keyword">I</span>/MainActivity: testFunction: threadId:<span class="hljs-number">1</span> <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">07</span>:<span class="hljs-number">20</span>:<span class="hljs-number">18.123</span> <span class="hljs-number">22734</span>-<span class="hljs-number">22755</span>/? <span class="hljs-keyword">D</span>/MainActivity: call: threadId:<span class="hljs-number">180</span> <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">07</span>:<span class="hljs-number">20</span>:<span class="hljs-number">18.142</span> <span class="hljs-number">22734</span>-<span class="hljs-number">22734</span>/? <span class="hljs-keyword">D</span>/MainActivity: onNext: threadId:<span class="hljs-number">1</span> <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">07</span>:<span class="hljs-number">20</span>:<span class="hljs-number">18.142</span> <span class="hljs-number">22734</span>-<span class="hljs-number">22734</span>/? <span class="hljs-keyword">I</span>/MainActivity: onNext: s = Hello World! <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">07</span>:<span class="hljs-number">20</span>:<span class="hljs-number">18.143</span> <span class="hljs-number">22734</span>-<span class="hljs-number">22734</span>/? <span class="hljs-keyword">D</span>/MainActivity: onCompleted: threadId:<span class="hljs-number">1</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
看見了沒,第二行log日誌threadId與其它的threadId很明顯的不同啊,說明咱們在處理事件的時候,發生在了一個新的線程裏面,而結果的接收,仍是在主線程裏面操做的。怎麼樣,只要添加兩句話,異步立馬就實現了,異步處理耗時操做,就是這麼easy。
咱們簡單看下源碼
<code class="hljs java has-numbering"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Observable<T> <span class="hljs-title">subscribeOn</span>(Scheduler scheduler) { <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span> <span class="hljs-keyword">instanceof</span> ScalarSynchronousObservable) { <span class="hljs-keyword">return</span> ((ScalarSynchronousObservable<T>)<span class="hljs-keyword">this</span>).scalarScheduleOn(scheduler); } <span class="hljs-keyword">return</span> create(<span class="hljs-keyword">new</span> OperatorSubscribeOn<T>(<span class="hljs-keyword">this</span>, scheduler)); }</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>
什麼意思呢?
Scheduler scheduler參數就是執行訂閱操做,返回一個源觀察到的修改,使其訂閱發生在指定的線程
observeOn(AndroidSchedulers.mainThread())
句話說,observeOn() 指定的是它以後的操做所在的線程。
打印字符串數組 from和just方式
以上是RxJava的很基礎很簡單的一個用法,那麼咱們接着往下看,好比咱們有一組需求把一個String數組的字符串,單個打印出來,咱們用Rxjava怎麼實現呢?看代碼:
<code class="hljs java has-numbering">Log.i(TAG, <span class="hljs-string">"testFunction: threadId:"</span> + Thread.currentThread().getId()); Observable.from(<span class="hljs-keyword">new</span> String[]{<span class="hljs-string">"one"</span>,<span class="hljs-string">"two"</span>,<span class="hljs-string">"three"</span>,<span class="hljs-string">"four"</span>}) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(<span class="hljs-keyword">new</span> Observer<String>() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCompleted</span>() { Log.d(TAG, <span class="hljs-string">"onCompleted: threadId:"</span> + Thread.currentThread().getId()); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span>(Throwable e) { Log.e(TAG, <span class="hljs-string">"onError: threadId:"</span> + Thread.currentThread().getId()); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onNext</span>(String s) { Log.i(TAG, <span class="hljs-string">"onNext: s = "</span> + s); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul>
看Log輸出日誌以下:
<code class="hljs http has-numbering"><span class="hljs-attribute">testFunction</span>: <span class="hljs-string">threadId:1</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">s = one</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">s = two</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">s = three</span> <span class="hljs-attribute">onNext</span>: <span class="hljs-string">s = four</span> <span class="hljs-attribute">onCompleted</span>: <span class="hljs-string">threadId:1</span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
From操做符用來將某個對象轉化爲Observable對象,而且依次將其內容發射出去。這個相似於just,可是just會將這個對象整個發射出去。好比說一個含有3個字符串的數組,使用from就會發射4次,每次發射一個數字,而使用just會發射一次來將整個的數組發射出去。
<code class="hljs lasso has-numbering"> <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"testFunction: threadId:"</span><span class="hljs-subst">+</span><span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); Observable<span class="hljs-built_in">.</span>just(<span class="hljs-string">"one"</span>, <span class="hljs-string">"two"</span>, <span class="hljs-string">"three"</span>, <span class="hljs-string">"four"</span>) <span class="hljs-built_in">.</span>subscribeOn(Schedulers<span class="hljs-built_in">.</span>io()) <span class="hljs-built_in">.</span>observeOn(AndroidSchedulers<span class="hljs-built_in">.</span>mainThread()) <span class="hljs-built_in">.</span>subscribe(<span class="hljs-literal">new</span> Observer<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onCompleted() { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>d(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onCompleted: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onError(Throwable e) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>e(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onError: threadId:"</span> <span class="hljs-subst">+</span> <span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); } @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> onNext(<span class="hljs-built_in">String</span> s) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"onNext: s = "</span> <span class="hljs-subst">+</span> s); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul>
<code class="hljs mathematica has-numbering"><span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.743</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">I</span>/MainActivity: testFunction: threadId:<span class="hljs-number">1</span> <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.784</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">I</span>/MainActivity: onNext: s = one <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.785</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">I</span>/MainActivity: onNext: s = two <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.785</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">I</span>/MainActivity: onNext: s = three <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.785</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">I</span>/MainActivity: onNext: s = four <span class="hljs-number">03</span>-<span class="hljs-number">08</span> <span class="hljs-number">08</span>:<span class="hljs-number">09</span>:<span class="hljs-number">25.785</span> <span class="hljs-number">32155</span>-<span class="hljs-number">32155</span>/? <span class="hljs-keyword">D</span>/MainActivity: onCompleted: threadId:<span class="hljs-number">1</span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
一對一轉換
<code class="hljs lasso has-numbering"> <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"testFunction: threadId:"</span><span class="hljs-subst">+</span><span class="hljs-keyword">Thread</span><span class="hljs-built_in">.</span>currentThread()<span class="hljs-built_in">.</span>getId()); Observable<span class="hljs-built_in">.</span>just(<span class="hljs-string">"1"</span>, <span class="hljs-string">"2"</span>, <span class="hljs-string">"3"</span>, <span class="hljs-string">"4"</span>) <span class="hljs-built_in">.</span>subscribeOn(Schedulers<span class="hljs-built_in">.</span>io()) <span class="hljs-built_in">.</span>observeOn(AndroidSchedulers<span class="hljs-built_in">.</span>mainThread()) <span class="hljs-built_in">.</span><span class="hljs-built_in">map</span>(<span class="hljs-literal">new</span> Func1<span class="hljs-subst"><</span><span class="hljs-built_in">String</span>, <span class="hljs-built_in">Integer</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-built_in">Integer</span> call(<span class="hljs-built_in">String</span> s) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: s = "</span><span class="hljs-subst">+</span>s); <span class="hljs-keyword">return</span> <span class="hljs-built_in">Integer</span><span class="hljs-built_in">.</span>parseInt(s); } }) <span class="hljs-built_in">.</span>subscribe(<span class="hljs-literal">new</span> Action1<span class="hljs-subst"><</span><span class="hljs-built_in">Integer</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> call(<span class="hljs-built_in">Integer</span> <span class="hljs-built_in">integer</span>) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: integer = "</span><span class="hljs-subst">+</span><span class="hljs-built_in">integer</span>); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li></ul>
<code class="hljs http has-numbering"><span class="hljs-attribute">testFunction</span>: <span class="hljs-string">threadId:1</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">s = 1</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">integer = 1</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">s = 2</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">integer = 2</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">s = 3</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">integer = 3</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">s = 4</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">integer = 4</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
簡單說一下Func1,其中的T表示傳入的參數類型,R表示方法返回的參數類型。源碼以下:
<code class="hljs php has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Func1</span><<span class="hljs-title">T</span>, <span class="hljs-title">R</span>> <span class="hljs-keyword">extends</span> <span class="hljs-title">Function</span> {</span> R call(T t); } </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
上例中還有一個叫作 Action1的類。也是 RxJava 的一個接口,用於包裝含有無參數的方法。 Func1 和 Action 的區別在於, Func1 包裝的是有返回值的方法。另外,和 ActionX 同樣, FuncX 也有多個,用於不一樣參數個數的方法。FuncX 和 ActionX 的區別在 FuncX 包裝的是有返回值的方法。
能夠看到,map() 方法將參數中的 String 對象轉換成一個 Integer對象後返回,而在通過 map() 方法後,事件的參數類型也由 String 轉爲了 Integer。這種直接變換對象並返回的,是最多見的也最容易理解的變換。不過 RxJava 的變換遠不止這樣,它不只能夠針對事件對象,還能夠針對整個事件隊列,這使得 RxJava 變得很是靈活。
封裝Observable一對多轉換
map轉換,是一對一的轉換,像示例當中,咱們把string轉成int,可是當咱們須要一對多的轉換,該怎麼作呢?好比說,定義一個學生類:
<code class="hljs cs has-numbering">package com.example.administrator.myapplication; import java.util.List; <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Student { <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) { <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-comment"><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span><span class="hljs-xmlDocTag">///</span>//</span> <span class="hljs-keyword">private</span> List<String> courses; <span class="hljs-keyword">public</span> List<String> <span class="hljs-title">getCourses</span>() { <span class="hljs-keyword">return</span> courses; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setCourses</span>(List<String> courses) { <span class="hljs-keyword">this</span>.courses = courses; } }</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul>
<code class="hljs lasso has-numbering"> Student student1 <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> Student(); student1<span class="hljs-built_in">.</span>setName(<span class="hljs-string">"safly"</span>); <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> courses <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> ArrayList<span class="hljs-subst"><></span>(); courses<span class="hljs-built_in">.</span>add(<span class="hljs-string">"語文"</span>); courses<span class="hljs-built_in">.</span>add(<span class="hljs-string">"數學"</span>); courses<span class="hljs-built_in">.</span>add(<span class="hljs-string">"英語"</span>); student1<span class="hljs-built_in">.</span>setCourses(courses); Student student2 <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> Student(); student2<span class="hljs-built_in">.</span>setName(<span class="hljs-string">"wyf"</span>); <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> courses2 <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> ArrayList<span class="hljs-subst"><></span>(); courses2<span class="hljs-built_in">.</span>add(<span class="hljs-string">"化學"</span>); courses2<span class="hljs-built_in">.</span>add(<span class="hljs-string">"地理"</span>); courses2<span class="hljs-built_in">.</span>add(<span class="hljs-string">"政治"</span>); student2<span class="hljs-built_in">.</span>setCourses(courses2); Observable<span class="hljs-built_in">.</span>just(student1,student2) <span class="hljs-built_in">.</span>subscribe(<span class="hljs-literal">new</span> Action1<span class="hljs-subst"><</span>Student<span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> call(Student student) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: name = "</span><span class="hljs-subst">+</span>student<span class="hljs-built_in">.</span>getName()); <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> course <span class="hljs-subst">=</span> student<span class="hljs-built_in">.</span>getCourses(); for(<span class="hljs-built_in">String</span> str:course){ <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: str = "</span><span class="hljs-subst">+</span>str); } } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li></ul>
這裏咱們沒有進行轉換,直接just發送過來,沒有用到轉換,而後在call中進行直接輸出了
<code class="hljs http has-numbering"><span class="hljs-attribute">call</span>: <span class="hljs-string">name = safly</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 語文</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 數學</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 英語</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">name = wyf</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 化學</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 地理</span> <span class="hljs-attribute">call</span>: <span class="hljs-string">str = 政治</span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
咱們用轉換來試試看?
這裏咱們用到了flatmap這一函數,按通俗的一點理解:咱們首先把Student轉成了Observable,而後呢,又把student.getCourses()轉成string挨個打印出來,結果以下:
<code class="hljs lasso has-numbering"> Observable<span class="hljs-built_in">.</span>just(student1,student2) <span class="hljs-built_in">.</span>flatMap(<span class="hljs-literal">new</span> Func1<span class="hljs-subst"><</span>Student, Observable<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">>></span>() { @Override <span class="hljs-keyword">public</span> Observable<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> call(Student student) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"Observable "</span> ); <span class="hljs-keyword">return</span> Observable<span class="hljs-built_in">.</span>from(student<span class="hljs-built_in">.</span>getCourses()); } }) <span class="hljs-built_in">.</span>subscribe(<span class="hljs-literal">new</span> Action1<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>() { @Override <span class="hljs-keyword">public</span> <span class="hljs-literal">void</span> call(<span class="hljs-built_in">String</span> s) { <span class="hljs-keyword">Log</span><span class="hljs-built_in">.</span>i(<span class="hljs-built_in">TAG</span>, <span class="hljs-string">"call: s = "</span><span class="hljs-subst">+</span>s); } });</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul>
輸出
<code class="hljs vbnet has-numbering"> Observable <span class="hljs-keyword">call</span>: s = 語文 <span class="hljs-keyword">call</span>: s = 數學 <span class="hljs-keyword">call</span>: s = 英語 Observable <span class="hljs-keyword">call</span>: s = 化學 <span class="hljs-keyword">call</span>: s = 地理 <span class="hljs-keyword">call</span>: s = 政治<span id="transmark"></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
咱們還記得Observable.from嘛?
From操做符用來將某個對象轉化爲Observable對象
<code class="hljs cs has-numbering"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <T> Observable<T> <span class="hljs-title">from</span>(Iterable<? extends T> iterable) { <span class="hljs-keyword">return</span> create(<span class="hljs-keyword">new</span> OnSubscribeFromIterable<T>(iterable)); }</code>