• Very easy to leak memory if not used correctly • Not truly concurrent: A single background threads processes multiple AsyncTasks sequentially • Clumsy API • Cancellation must be handled manually • Error handling is difficult
Looper and a Handler, they can be used to process tasks sequentially • Associated Handler has a mailbox to which tasks (messages) can be sent • Messages are processed one by one by the Looper • Thread is kept alive even when the mailbox is empty • Must be explicitly stopped
• Cancellation must be handled manually • Multiple Handler Threads are required for processing tasks concurrently • The more Handler Threads you create, the more of them you have to manage. • Work scheduling must be handled manually • Difficult to use given its primitive nature • Error handling is difficult
• Applicable outside the Android world as well • Easy to distribute and share work between thread pools Disadvantages • Cancellation must be handled manually • The most useful APIs are only available for >= API 24 • Error handling is difficult.
Extensions: a library for composing asynchronous and event based programs by using observable sequences. Reactive Extensions has a variety of implementations in different languages. Besides RxJava, there's also RxJS, RxPy, RxSwift, and others.
be applied on an event stream to transform it. • The transformed event stream can then be subscribed-to, to get notifications on each emission. • The manipulation and observation of streams can be done on different threads seamlessly.
Cold streams: Do not begin execution unless there is at least one subscriber listening ◦ Hot streams: These streams are always running, no matter if someone is listening or not. • During the flow of a stream, any errors are propagated downstream and can be handled by the subscriber. • Cancellation is easy.
as they are created • Emit items even when no one is listening • Used to model sources of events which occur regardless of listeners, such as User Input events and Location Updates Cold Streams • Only begin execution when someone starts listening • Useful for modelling event sources which should not run if no one is listening, such as database query results
They are cold in nature, ie, they begin execution only when someone starts listening to them • Observables can be observed by observers. • Observers are notified whenever the Observable emits an event
in a stream, applies some method on the data in the stream, and returns a new one. • RxJava has a rich set of operators to cater to every need • Writing your own operators is difficult, but possible • Always look for existing operators thoroughly before writing your own
applying a function to each item • Essentially converts an Observable of one type into an observable of another type • The items in the input observable are transformed to the output type by the mapping function
a new Observable, then flatten the emissions from each new Observable into a single one. • Each input item is mapped to create an Observable. • Then the items of each of these mapped Observables are flattened into a single observable.
threading • A Scheduler is like a pool of threads • There are different schedulers suited for different tasks • The types of Schedulers are: ◦ IO ◦ Computation ◦ Single-Threaded ◦ Trampoline
a thread of execution can be chosen: subscribeOn(), and observeOn() • subscribeOn() controls the thread on which the source starts emitting • observeOn() controls the thread on which the subscriber receives the results. • subscribeOn() affects things upstream, while observeOn() affects things downstream
get results of a stream on the Android Main Thread • To do this, we need to add a dependency on the RxAndroid library • With the dependency added, the Android Main Thread can be used with RxJava in this manner:
Observer • An observer contains various callback methods, but the most important ones are: onNext() and onError() • Whenever an observable emits a value, the onNext() method is called using the latest value • Whenever an error occurs, the onError() method is called and the stream is terminated
following condition is met: • All values provided by twitterUsernames() have been emitted, or • An error occurs in the stream somewhere, or • We cancel the subscription early
functions. • Pure functions take in an input and provide an output. They don’t affect things outside their scope. • This means that pure functions can not modify the state of an application. • Therefore an application can’t just be composed of pure functions.
we use Side Effects • Unlike pure functions, they only exist to modify things outside their scope. • Side Effects and pure functions together make up an application
• The entire state of the stream is contained inside it. • A stream does not affect the outside world, it just operates on its own data • RxJava has support for side-effects to affect things outside the stream
by Google. Unlike its alternatives such as Guice, Dagger is fully static and works at compile time. It uses annotation processing to generate the dependency graph and factories at compile time.
an object’s dependencies are provided to it, rather than letting the object create them. • It has many benefits, including flexibility and easier testing. • Helps us write better code and create a better architecture.
Injection, and Field injection. • We should always prefer Constructor injection where possible. • In places where we do not control the constructors (such as Android Activities/Fragments), we use field Injection.
need to annotate their constructors with @Inject • For each class with an @Inject constructor, Dagger adds it to our dependency graph. • If our class needs a dependency from this graph, Dagger can automatically provide it for us.
about external dependencies our application will need. • Modules are classes/abstract classes/interfaces annotated with @Module • Modules contain static methods which create and return external dependencies • Each such method must be annotated with @Provides • For methods that only bind dependencies, we must use @Binds • Methods with @Binds can be abstract
which tell Dagger to build the object graph. • Components are the ones which actually satisfy dependencies in our application. • Components depend on Modules which provide external dependencies. • If we create a component named AppComponent, Dagger will generate its implementation with the name DaggerAppComponent.
using field injection. • Components can also be used to fetch dependencies outside of a constructor. This makes Dagger also work as a Service Locator. • Components need to be initialized when our Application is created.