Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RxJava2 support #668

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DaoCore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
compileOnly 'com.google.android:support-v4:r7'

compileOnly 'io.reactivex:rxjava:1.1.8'
compile 'io.reactivex.rxjava2:rxjava:2.0.9'

compileOnly files('libs/sqlcipher.jar')
}
Expand Down
32 changes: 32 additions & 0 deletions DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.rx.RxDao;
import org.greenrobot.greendao.rx2.Rx2Dao;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -73,6 +74,9 @@ public abstract class AbstractDao<T, K> {
private volatile RxDao<T, K> rxDao;
private volatile RxDao<T, K> rxDaoPlain;

private volatile Rx2Dao<T, K> rx2Dao;
private volatile Rx2Dao<T, K> rx2DaoPlain;

public AbstractDao(DaoConfig config) {
this(config, null);
}
Expand Down Expand Up @@ -964,6 +968,34 @@ public RxDao<T, K> rx() {
return rxDao;
}

/**
* The returned Rx2Dao is a special DAO that let's you interact with Rx Observables without any Scheduler set
* for subscribeOn.
*
* @see #rx2()
*/
@Experimental
public Rx2Dao<T, K> rx2Plain() {
if (rx2DaoPlain == null) {
rx2DaoPlain = new Rx2Dao<>(this);
}
return rx2DaoPlain;
}

/**
* The returned Rx2Dao is a special DAO that let's you interact with Rx Observables using RX's IO scheduler for
* subscribeOn.
*
* @see #rx2Plain()
*/
@Experimental
public Rx2Dao<T, K> rx2() {
if (rx2Dao == null) {
rx2Dao = new Rx2Dao<>(this, io.reactivex.schedulers.Schedulers.io());
}
return rx2Dao;
}

/** Gets the SQLiteDatabase for custom database access. Not needed for greenDAO entities. */
public Database getDatabase() {
return db;
Expand Down
57 changes: 57 additions & 0 deletions DaoCore/src/main/java/org/greenrobot/greendao/rx2/Rx2Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.greenrobot.greendao.rx2;

import org.greenrobot.greendao.annotation.apihint.Experimental;
import org.greenrobot.greendao.annotation.apihint.Internal;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.Scheduler;

/**
* Created by Zhang Tingkuo.
* Date: 2017-04-28
* Time: 14:14
*/
@Internal
class Rx2Base {

protected final Scheduler mScheduler;

/**
* No default scheduler.
*/
public Rx2Base() {
mScheduler = null;
}

/**
* Sets the default scheduler, which is used to configure returned observables with
* {@link Observable#subscribeOn(Scheduler)}.
*/
@Experimental
Rx2Base(Scheduler scheduler) {
mScheduler = scheduler;
}

/**
* The default scheduler (or null) used for wrapping.
*/
@Experimental
public Scheduler getScheduler() {
return mScheduler;
}

protected <R> Observable<R> wrap(Callable<R> callable) {
return wrap(Rx2Utils.fromCallable(callable));
}

protected <R> Observable<R> wrap(Observable<R> observable) {
if (mScheduler != null) {
return observable.subscribeOn(mScheduler);
} else {
return observable;
}
}

}
Loading