Exemplo n.º 1
0
/**
 * 数据库操作抽象类,封装数据库操作、回调流程
 *
 * @param <T> 操作的数据类型,一般为List的数据集合
 */
public abstract class AbsDbAPI<T> {
  DatabaseExecutor mDbDispatcher = DatabaseExecutor.getExecutor();
  Handler mHandler = mDbDispatcher.getUIHandler();

  /**
   * 提交数据库命令
   *
   * @param cmd
   */
  public void submit(DbCommand cmd) {
    mDbDispatcher.submit(cmd);
  }

  protected void deliverResult(final SimpleFetchListener<T> listener, final T t) {
    mHandler.post(
        new Runnable() {

          @Override
          public void run() {
            listener.onComplete(t);
          }
        });
  }

  protected void deliverResultForCount(
      final SimpleFetchListener<Integer> listener, final Integer count) {
    mHandler.post(
        new Runnable() {

          @Override
          public void run() {
            listener.onComplete(count);
          }
        });
  }

  /** 数据库命令类,封装事务处理,提升效率 */
  public abstract static class DbCommand implements Runnable {
    @Override
    public final void run() {
      ActiveAndroid.beginTransaction();
      execute();
      ActiveAndroid.setTransactionSuccessful();
      ActiveAndroid.endTransaction();
    }

    protected abstract void execute();
  }
}
Exemplo n.º 2
0
 /**
  * 提交数据库命令
  *
  * @param cmd
  */
 public void submit(DbCommand cmd) {
   mDbDispatcher.submit(cmd);
 }