Skip to content

Reactive programming with RxJava Support

xcesco edited this page Nov 29, 2017 · 9 revisions

Reactive programming is one of programming trends of this moment. If you want to get more information about it just visit the following site:

The main scope of Rx library is simplify asynchronous tasks. In Android platform development, Rx library is a valid replacement of AsynchTask.

Kripton library contains suppports AsyncTask and Rx too. When you want to enable Rx supports in SQLite you have to:

  • add in gradle configuration Rx library references:
dependencies {
 implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
 implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
}
  • enable in data source generation with attribute rx=true in @BindDataSource.
@BindDataSource(daoSet = {ContactDao.class}, fileName = "contact.db", rx = true)
public interface ContactDataSource {
}

And now?

Once Rx Kripton support is enabled, when a datasource is generated, it contains:

  • A subject for each table: this feature allows to register an async listener to monitor table changes. When an insert/update/delete operation is executed on the associated table, an event was fired. This event is trapped by subject.
  • Executed methods to execute transaction or simply batch operations with Observable, Flowable, Single, Maybe.

Examples

Just for example. Suppose we have an application that manages Contact. So, we have to create a Contact entity, Dao Contact interface and Data Source interface.

Contact.class

@BindTable
public class Contact {
    public long id;
    public String name;
    public String surname;
}

The dao interface DaoContact.class

public interface ContactDao {
    @BindSqlInsert
    void insert(Contact bean);

    @BindSqlUpdate(where="id=${bean.id}")
    void update(Contact bean);

    @BindSqlDelete(where="id=${bean.id}")
    void delete(Contact bean);

    @BindSqlSelect(where="id=${id}")
    Contact selectById(long id);

    @BindSqlSelect
    List<Contact> selectAll();
}

And ContactDataSource.class

@BindDataSource(daoSet = {ContactDao.class}, fileName = "contact.db", rx = true)
public interface ContactDataSource {
}

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally