RxBolts

Reactive Bolts-Android. Allow convert Task<T> to Observable<T>.

Usage

TaskObservable.defer():

Task<String> helloTask = Task.forResult("Hello, world!");
TaskObservable.defer(() -> helloTask).subscribe(it -> {
System.out.println(it);
});

Take 5 users using Parse:

TaskObservable.defer(() -> ParseUser.getQuery().findInBackground()).take(5).subscribe(user -> {
System.out.println(user.getObjserId());
});

Update location of 5 users using Parse:

TaskObservable.defer(() -> ParseUser.getQuery().findInBackground()).take(5).flatMap(user -> {
user.put("location", "Taipei");
return TaskObservable.defer(() -> user.saveInBackground());
}).subscribe();

Null task handling

TaskObservable.defer():

Task<String> nullTask = Task.forResult(null);
TaskObservable.defer(() -> nullTask).subscribe(it -> {}, e -> {}, () -> {
System.out.println("onCompleted");
});

TaskObservable.deferNullable():

Task<String> nullTask = Task.forResult(null);
TaskObservable.deferNullable(() -> nullTask).subscribe(it -> {
System.out.println(it); // print null
});

TaskObservable.deferNullable() for printing updated location of 5 users:

TaskObservable.defer(() -> ParseUser.getQuery().findInBackground()).take(5).flatMap(user -> {
user.put("location", "Taipei");
// ParseUser.saveInBackground() returns Task<Void> conatins null
// Allow omit null with TaskObservable.deferNullable()
return TaskObservable.deferNullable(() -> user.saveInBackground()).map(it -> user);
}).subscribe(user -> {
System.out.println(user.getObjectId());
});

TaskObservable.deferNonNull():

Task<String> nullTask = Task.forResult(null);
TaskObservable.deferNonNull(() -> nullTask).subscribe(it -> {}, e -> {
e.printStackTrace(); // NullPointerException
});

Error handling

Failed:

Task<String> failedTask = Task.forError(new RuntimeException("An error message."));
TaskObservable.defer(() -> failedTask).subscribe(it -> {}, e -> {
e.printStackTrace(); // RuntimeException
});

Sample code

Installation

via jcenter

repositories {
jcenter()
}
dependencies {
compile 'com.infstory:rxbolts:1.0.0'
}

Or via jitpack.io

repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.yongjhih:rxbolts:1.0.0'
}

ref.

origin from: https://github.com/yongjhih/RxBolts