Rxjava +EventBus 数据库的异步操作和界面的刷新

1,数据库的操作核心代码:
以插入数据库为例,本文重点在与数据库的操作,相关数据库的创建不做讨论

private SqliteOpearRx(Context context) {
        this.context = context;
        mContentResolver = this.context.getContentResolver();
        dbHelper = DBHelper.getInstance(context);
    }

    /**
     * 初始化
     * 
     * @param context
     * @return
     */
    public static SqliteOpearRx getInstance(Context context) {
        if (mSqliteOpearRx == null) {
            synchronized (SqliteOpearRx.class) {
                if (mSqliteOpearRx == null) {
                    mSqliteOpearRx = new SqliteOpearRx(context);
                }
            }
        }
        return mSqliteOpearRx;
    }
//上层直接调用的函数
/**
     * 插入数据库
     * 
     * @param ObjectBean
     * @return
     */
    public Observable useInsertMessage(MessageBean ObjectBean) {
    // 逻辑执行在异步线程中
        return makeObservable(insertSqlMessage(ObjectBean)).subscribeOn(Schedulers.computation());
    }
**
     * 
     * @param mMessageBean
     * @return
     */
    private Callable insertSqlMessage(final MessageBean mMessageBean) {
        return new Callable() {
            @Override
            public Integer call() {
                // 模板代码插入数据库的具体代码
                int fileType = mMessageBean.getFileType();
                ContentValues values = new ContentValues();
                Uri insertRow = mContentResolver.insert(AllMessageProvider.CONTENT_URI, values);
                /此处还有一个小知识点,以插入数据库的id作为bean的唯一标识,避免数据的重复
                int id = (int) ContentUris.parseId(insertRow);
                return id;
            }
        };
    }

//核心的转化函数  实现callback函数转为observable
private  Observable makeObservable(final Callable func) {
        return Observable.create(new Observable.OnSubscribe() {

            @Override
            public void call(Subscribersuper T> subscriber) {
                try {
                    subscriber.onNext(func.call());

                } catch (Exception ex) {

                    Log.e(TAG, "Error reading from the database", ex);

                }

            }
        });
    }

2,作为工具调用函数

 SqliteOpearRxmDbHelper = SqliteOpearRx.getInstance(mContext);
    //返回结果在主线程中处理
   mDbHelper.useInsertMessage(nImBean).observeOn(AndroidSche dulers.mainThread()).subscribe(new Action1() {
                            @Override
                            public void call(Integer id) {
                                if (id != -1) {
                                    Log.d(TAG, "接收到的信息插入数据库成功 id" + id);
                                    if (nImBean.getMsrpId() == null) {
                                        nImBean.setMsrpId(id + "");
                                    }
                                    nImBean.setId(id);
                                    nImBean.setMsgOpera(MessageBean.MESSAGE_OPERA_ADD);
                                    //通知界面进行刷新
                                    EventBus.getDefault().post(nImBean);
                                }
                            }
                        });
3.Activity中接收查询的结果进行界面的刷新

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy()");
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

@Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           Log.d(TAG, "onCreate");
        EventBus.getDefault().register(this);
    }
@Subscribe(threadMode = ThreadMode.MAIN)
    public void getEvent(MessageBean bean) {
//处理自己的界面刷新的逻辑
                + 

}



“`

你可能感兴趣的:(学习笔记,工作总结)