Beispiel #1
0
  public static void saveToDatabase(Context context, final List<Account> items) {
    final DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);

    try {
      final Dao<Account, String> dao = helper.getAccountDao();
      dao.callBatchTasks(
          new Callable<Void>() {

            public Void call() throws Exception {
              // Delete all
              int removed = dao.delete(dao.deleteBuilder().prepare());
              if (Flags.DEBUG) {
                Log.d(LOG_TAG, "Deleted " + removed + " from database");
              }

              for (Account item : items) {
                dao.create(item);
              }
              if (Flags.DEBUG) {
                Log.d(LOG_TAG, "Inserted " + items.size() + " into database");
              }
              return null;
            }
          });
    } catch (Exception e) {
      if (Flags.DEBUG) {
        e.printStackTrace();
      }
    } finally {
      OpenHelperManager.releaseHelper();
    }
  }
Beispiel #2
0
  public static List<Account> getFromDatabase(Context context) {
    final DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
    List<Account> items = null;

    try {
      final Dao<Account, String> dao = helper.getAccountDao();
      items = dao.query(dao.queryBuilder().prepare());
    } catch (SQLException e) {
      if (Flags.DEBUG) {
        e.printStackTrace();
      }
    } finally {
      OpenHelperManager.releaseHelper();
    }

    return items;
  }