Database, I/O operation and CPU intensive operations required to be done in other thread than main one. On Android platform there are many ways to manage background threads. I suppose you already known what does it means: thread, runnable, async task and so on…

Moreover, in the last years, one library became very popular to manage ansync operation: RX, a library that implements reactive programming. For just a refresh of how you can use RX library in Android development, you can see this tutorial, by Vogella.

Kripton Persistence Library has supports for generation code about async task and RX supports. Suppose to create an application to manage phone numbers. The managed entity is so defined:

The associated DAO interface definition is:

Works with Async Task

Code generation for async task is simpler as set to true flag asyncTask in data source generation.

When asyncTask is true, an async task wrapper to simply asynchronous operation with PhoneDataSource will be generated. The name of the wrapper is the same of datasource, but the DataSource prefix is replaced with AsyncTask prefix. For PhoneDataSource it will be BindPhoneAsyncTask. This class is an abstract class, so you need to implements onExecute and onFinish methods.

The method execute can be run multiple times unlike the “normal” async tasks.

You can also override methods onPreExecute and onProgressUpdate. Usually async task was used to read data from database, so they open read only connections. If you need to use a writable connection, just specify it in the constructor.

There is a inner class named Simple generated to further simplify decleration of async task.

Works with RX

Reactive programming is one of the coolest library around. Since version 3.x Kripton Persistence Library support RX integration. First of all, to enable RX support in Kripton, just add RX libraries to gradle configuration

To enable RX support code generation, just use rx attribute in @BindDataSource.

Kripton, with RX support allows to:

  • manage batch operations and transactions with RX methods.
  • generate a Subject for each table. Every time an update/delete/insert operation is done on a table, an event will be fired.

.Just for example, the following code will use to asynchronous insert some data in phone number table in the Scheduler.io thread. The subscriber will be executed in main thread.

In the above example it was used Java 1.8 language level to get a more elegant code. The minimum source code level is 1.7, so you can write same code without using lambda expressions. To write RX transaction:

With RX support, Kripton allows to use this reactive programming to monitor add/remove/update table events. Every Dao definition will expose a Subject with method subject.

Conclusion

Async operations are big tasks to deal. Kripton allows to manage them with async task and reactive programming support. For any question or information does not esitate to write comment on this post.

 

Happy coding!

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close