Reputation: 2884
In my android application, I have various "entities" such as user defined. I'm using a single DbOperations class that has the default Select
, Insert
, Update
and Delete
functionality.
An async task is used as an intermediary. It sits in between my entities and DbOperations class and performs everything asynchronously. Here's an example.
ASYNC CLASS - with Insert Method code
private DbResponse InsertUser() {
ContentValues cntValues = GetCrmUserContentVal();
long result = _dbConn.InsertRecord(cntValues, TABLE_NAME);
DbResponse dbResponse = new DbResponse();
if(result == -1)
{
dbResponse.setStatus(false);
}
else {
dbResponse.setStatus(true);
dbResponse.setID(result);
}
return dbResponse;
}
CRM USER Entity Class - Insert Method
public void InsertintoDb()
{
new CRMUserDbOperations(this,this,DbOperationType.Insert,getCurrentContext()).execute();
}
DbResponse - Return type class is a seperate class -
private Boolean Status;
private String ErrorMessage;
private Cursor Data;
private long ID;
private DbOperationType dbOpType;
In the doBackground process of the async task, I have this switch code -
switch (_DbOpType) { // Enum type.
case Insert:
dbResponse = InsertUser();
break;
case Select:
dbResponse = SelectUser();
break;
case Update:
dbResponse = UpdateUser();
break;
default:
throw new InvalidParameterException(
_Context.getString(R.string.invalid_io));
}
As you can notice this asynchronous task has code for all the various operations I might have to perform on the entity. For other entities, I'll have the same structure as well...
Now my question is, could I be doing this in a better manner?
Upvotes: 1
Views: 268
Reputation: 17284
Yes it can be done in a better way. Let me give you an example of how we are handling it in our current app. You just need 4 AsyncTask
s in total for insert, update, delete and select operations. Let me give you an example.
You have an interface and every entity will implement it:
public interface DbOps {
public int delete(String where);
public void insert();
public void update();
public Cursor select();
}
NOTE: Arguments and return types will be of your choice that fits your need but must also fit for every entity class. I am going to use delete()
method as an example.
Now you need only one DeleteTask
for all entitites:
private static class DeleteTask extends AsyncTask<String, Void, Integer> {
private final DbOps mOps;
public RowDeleteTask(DbOps ops) {
mOps = ops;
}
@Override
protected Integer doInBackground(String... wheres) {
String where = wheres[0];
int rowsDeleted = mOps.delete(where);
return rowsDeleted;
}
}
Fire it like this:
new DeleteTask(mUserEntity).execute("id = 4");
new DeleteTask(mMoviesEntity).execute("name = x-man");
and obviously you will have something similar to this if we take UserEntitiy
for example:
public UserEntity implements DbOps{
@Override
public int delete(String where){
return _dbConn.delete(mTable, where, null);
}
.
.
.
}
Upvotes: 2
Reputation: 20557
This isn't product placement or anything, it is open source, I have been working on Async databases for a while now, and have recently created a library for it.
It is hosted on Github at http://fabiancook.github.io/AndroidDbHelper/
It covers a more general need for async database usage, you can either do one thing async if you want, or the whole lot.
It will have an implementation for a entity framework in the coming months as I am working on a Ubuntu touch version at this moment.
Any info needed just ask.
For small amounts of objects entities are great, but when you want to report on them they get really slow, which is even apparent in microsofts entity framework. For the most it is usually a heck of a lot faster (performance wise) to use straight SQL in an async way as it takes out the need for that middle object.
Note, between android 1.6 and 3.0 the AsyncTask
class would execute sometimes in parallel, which would cause some problems in any database. So when using those versions you there would have to some differences, this is being worked into my DbHelper :)
http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)
Upvotes: 1