Android GreenDao数据封装使用

GreenDao

这里只讲封装,基础的继承,建表就不在这写了,放个链接,不会的可以去看下。

https://www.jianshu.com/p/ec007010fa07

这里是P层的查询调用

 DbCheck.getInstance().getSqlList(0, "name").subscribe(new HttpSubscriber>(new SubscriberOnNextListener>() {
            @Override
            public void onSuccess(List beanEntities) {

            }

            @Override
            public void onError(int code, String errorMsg) {

            }
        }));

这是DbCheck


import java.util.List;
import com.example.rrm.MyApplication;
import com.example.rrm.greendao.gen.BeanEntity;
import com.example.rrm.greendao.gen.BeanEntityDao;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DbCheck {
    private volatile static DbCheck instance;

    public static DbCheck getInstance() {
        if (instance == null) {
            synchronized (DbCheck.class) {
                if (instance == null) {
                    instance = new DbCheck();
                }
            }
        }
        return instance;
    }

    public Observable> getSqlList(final int id, final String name) {
        return Observable.create(new ObservableOnSubscribe>() {
            @Override
            public void subscribe(ObservableEmitter> e) throws Exception {
                List beanEntityList = MyApplication.getInstances().getDaoSession().getBeanEntityDao()
                        .queryBuilder().where(BeanEntityDao.Properties.Id.eq(id), BeanEntityDao.Properties.Name.eq(name)).list();
                e.onNext(beanEntityList);
                e.onComplete();

            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

}

 

 

你可能感兴趣的:(面试问题系列)