rxjava--learned the next RxJava.

Source: Internet
Author: User

Before I knew there was Rxjava this thing, know this thing is very dick, but also stop to look at the title, see a few paragraph introduction of the degree (lazy cancer hairenbujian).
This week on the whim, the time to take the previous collection of the Rxjava of the Big God wrote the primer on the article read.

http://gank.io/post/560e15be2dca930e00da1083

Spent the night watching, by the way to take notes, and then try to upload and compress the image of the logic to use Rxjava implementation.
It's really cool to find out. The dick exploded. The whole process is finished in a breath, does not need to jump in the handler, but also comes with thread scheduling, awesome.

The following is a random note, some are directly ctrl+c,ctrl+v, remember the mess, it is estimated that I myself know what the ghost is written in this note (in fact, just for the water blog, Manual funny).

Download

Https://github.com/ReactiveX/RxJava
Https://github.com/ReactiveX/RxAndroid

The latest version when writing notes:
compile ‘io.reactivex:rxjava:1.0.14‘
compile ‘io.reactivex:rxandroid:1.0.1‘

Understand

Rxjava mainly uses the Observer pattern, 观察者 and 被观察者 by completing the 订阅 binding.
In the program, it is 观察者 not possible or bad to take the initiative (that is, timed queries, so wasteful performance, and not in time) to get 被观察者 the status.
Usually in the 被观察者 event of occurrence, then go to notice 观察者 . Although this is a bit different from the real situation.

Usage process

Implementation 被观察者 , binding事件
Implementation 观察者 , defining the response of an event
--Viewer 订阅 Events ( 被观察者 . Subscribe ( 观察者 ))
The subscriber starts sending events

Api Observable: The person being observed

To create() construct an object through, just() ,, from() to bind (enter) an event 被观察者
by subscribe() following 观察者 the parallel and starting to 观察者 send the event

The map() input event is converted (processed) into another event, returning the result to the observer response

flatMap(), the 可以 input event is converted (processed) into another event ( 1转1 , or 1转多 ), the new event is bound to the new, the new object is formed, 被观察者 被观察者 and the new object is returned 被观察者
(The event will then be 新被观察者 given 观察者 , so when you need to be aware that the subscribe() 观察者 response is the type of events, to be 新观察者 consistent)

lift()On the 目标订阅者 layer of packaging, through Observable.Operator , so that after packaging 订阅者 can be directly processed 事件 .
compose()is for multiple lift() packages

throttleFirst()Discard new events within a certain interval of time after each event trigger, use it as a shake-out, press once, and then press again for a few minutes.

Observer: Viewer

Main response Events
onNext(), onCompleted() andonError()
onCompleted()and onError() Mutex

Subscriber: Implements the ObserverThe abstract class
  • onStart(): This is the method of subscriber increase. It can be used to do some preparatory work, such as clearing 0 or resetting the data, before the subscribe is started and the event has not been sent. This is an optional method, and by default its implementation is empty. It is important to note that if the line threads requirements for the preparation work (such as a dialog box that shows progress, which must be performed on the main thread), OnStart () does not apply because it is always called on the thread that occurs in the subscribe and cannot be specified by the thread. To prepare for the specified thread, you can use the Doonsubscribe () method, which you can see later in this article.
  • unsubscribe(): This is the method that subscriber implements for another interface Subscription, which is used to unsubscribe. After this method is called, Subscriber will no longer receive events. In general, before this method is called, you can use isunsubscribed () to determine the state first. Unsubscribe () This method is important, because after subscribe (), Observable will hold a subscriber reference, if not released in time, there will be a risk of memory leaks. So it's a good idea to keep a principle: Call unsubscribe () in the right place (such as OnPause () OnStop (), and so on, as soon as you're no longer using it, to avoid a memory leak.
Action0Action1And ActionXObserverThe incomplete version

Action0Responds to only one event, corresponding toonCompleted()
Action1Respond to only one event, corresponding onNext() and onCompleted() , there are other Action2 , Action3 etc.

Observable.OnSubscribe: Subscribe

is actually 事件 the abstraction that 事件 will excite 观察者 , that is, 观察者 the invocation of the response callback

Scheduler: Line Program control system
1
2
3
4
5
6
7
8
 observable.just (1, 2, 3, 4) //IO thread, specified by Subscribeon () 
//new thread, specified by Observeon ()
map (MapOperator2) //IO thread, specified by Observeon ()
//Android main thread, specified by Observeon ()
  • Schedulers.immediate(): Runs directly on the current thread, which is equivalent to not specifying a thread. This is the default Scheduler.
  • Schedulers.newThread(): Always enable new threads and perform operations on new threads.
  • Schedulers.io(): Scheduler used by I/O operations (read-write files, read-write databases, network information interactions, etc.). The behavior pattern is almost the same as Newthread (), except that the internal implementation of IO () is the use of an unlimited thread pool that can reuse idle threads, so that Io () is more efficient in most cases than Newthread (). Do not put the calculation work in IO (), you can avoid creating unnecessary threads.
  • Schedulers.computation(): Calculates the Scheduler used. This calculation refers to CPU-intensive computations, i.e., operations that do not restrict performance by operations such as I/O, such as the calculation of a sample. This Scheduler uses a fixed thread pool with a size of CPU cores. Do not put the I/O operation in computation (), otherwise the wait time of the I/O operation wastes the CPU.
  • AndroidSchedulers.mainThread(), it specifies that the operation will run on the Android main thread.
    • subscribeOn()Specifies the thread on which the subscription event occurred (only once, first called)
    • observeOn()The thread that specifies the observer response, which can be called (in effect) multiple times
    • doOnSubscribe()Doonsubscribe () is followed by a Subscribeon (), which allows you to specify the thread that is ready to work.

doOnSubscribe()And Subscriber.onStart() is also executed after the subscribe () call and before the event is sent, but the difference is that it can specify the thread.
By default, Doonsubscribe () executes the thread that occurs in subscribe (), and if there is Subscribeon () after Doonsubscribe (), it executes the thread specified in the Subscribeon () closest to it.

1
2
3
4
5
6
7
8
9
10
11
Observable.create (Onsubscribe)
. Subscribeon (Schedulers.io ())
. Doonsubscribe (new Action0 () {
@Override
Call() {
Need to be executed on the main thread
}
})
Specify the main thread
. Observeon (Androidschedulers.mainthread ())
. Subscribe (subscriber);

rxjava--learned the next RxJava.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.