org.greenrobot.daocompat.rx.RxDao Maven / Gradle / Ivy
Show all versions of objectbox-daocompat Show documentation
/*
* Copyright (C) 2011-2017 Markus Junginger, greenrobot (http://greenrobot.org)
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.daocompat.rx;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import org.greenrobot.daocompat.AbstractDao;
import io.objectbox.annotation.apihint.Experimental;
import rx.Observable;
import rx.Scheduler;
/**
* Like {@link AbstractDao} but with Rx support. Most methods from AbstractDao are present here, but will return an
* {@link Observable}. Modifying operations return the given entities, so they can be further processed in Rx.
*
* Instances of RxDao may have an default {@link rx.Scheduler}, which is used to configure returned observables with
* {@link Observable#subscribeOn(Scheduler)} (see {@link AbstractDao#rxPlain()}, which uses the IO scheduler).
*
* Note: DO NOT call more than one data modification operation when you can use a transaction instead (see
* {@link RxTransaction}. Individual calls use a transaction each and are much slower.
*
* @param Entity type
* @param Primary key (PK) type; use Void if entity does not have exactly one PK
* @see AbstractDao#rxPlain()
*/
@Experimental
public class RxDao extends RxBase {
private final AbstractDao dao;
/**
* Creates a new RxDao without a default scheduler.
*/
@Experimental
public RxDao(AbstractDao dao) {
this(dao, null);
}
/**
* Creates a new RxDao with a default scheduler, which is used to configure returned observables with
* {@link Observable#subscribeOn(Scheduler)}.
*/
@Experimental
public RxDao(AbstractDao dao, Scheduler scheduler) {
super(scheduler);
this.dao = dao;
}
/**
* Rx version of {@link AbstractDao#loadAll()} returning an Observable.
*/
@Experimental
public Observable> loadAll() {
return wrap(new Callable>() {
@Override
public List call() throws Exception {
return dao.loadAll();
}
});
}
/**
* Rx version of {@link AbstractDao#loadAll()} returning an Observable.
*/
@Experimental
public Observable load(final Long key) {
return wrap(new Callable() {
@Override
public T call() throws Exception {
return dao.load(key);
}
});
}
/**
* Rx version of {@link AbstractDao#refresh(Object)} returning an Observable.
* Note that the Observable will emit the given entity back to its subscribers.
*/
@Experimental
public Observable refresh(final T entity) {
return wrap(new Callable() {
@Override
public T call() throws Exception {
dao.refresh(entity);
return entity;
}
});
}
/**
* Rx version of {@link AbstractDao#insert(Object)} returning an Observable.
* Note that the Observable will emit the given entity back to its subscribers.
*/
@Experimental
public Observable insert(final T entity) {
return wrap(new Callable() {
@Override
public T call() throws Exception {
dao.insert(entity);
return entity;
}
});
}
/**
* Rx version of {@link AbstractDao#insertInTx(Collection)} returning an Observable.
* Note that the Observable will emit the given entities back to its subscribers.
*/
@Experimental
public Observable> insertInTx(final Collection entities) {
return wrap(new Callable>() {
@Override
public Iterable call() throws Exception {
dao.insertInTx(entities);
return entities;
}
});
}
/**
* Rx version of {@link AbstractDao#insertInTx(Object[])} returning an Observable.
* Note that the Observable will emit the given entities back to its subscribers.
*/
@Experimental
public Observable