正确实施异步任务DbOperations的 [英] Proper implementation of DbOperations in Async Task
问题描述
在我的Android应用程序,我有不同的实体,如用户自定义。我使用具有默认选择
,插入
,更新<单DbOperations类/ code>和
删除
功能。
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.
这是异步任务被用作中介。它坐落在我的实体和DbOperations类之间异步执行的一切。下面是一个例子。
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.
异步CLASS - 用插入法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用户实体类 - 插入法
public void InsertintoDb()
{
new CRMUserDbOperations(this,this,DbOperationType.Insert,getCurrentContext()).execute();
}
DbResponse - 返回类型的类是一个独立的类 -
DbResponse - Return type class is a seperate class -
private Boolean Status;
private String ErrorMessage;
private Cursor Data;
private long ID;
private DbOperationType dbOpType;
在 doBackground 异步任务的过程中,我有这个开关code -
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));
}
正如你可以看到这个异步任务code所有我可能要对实体执行各种操作。对于其他实体,我将具有相同的结构,以及...
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?
推荐答案
是的,它可以以更好的方式来完成。让我给你的,我们是如何处理它在我们当前应用的例子。你只需要4 的AsyncTask
S IN总插入,更新,删除和选择操作。让我给你举个例子。
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.
现在,你只需要一个 DeleteTask活动
所有entitites:
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;
}
}
火这样的:
new DeleteTask(mUserEntity).execute("id = 4");
new DeleteTask(mMoviesEntity).execute("name = x-man");
和很明显,你将有类似这样的东西,如果我们采取 UserEntitiy
例如:
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);
}
.
.
.
}
这篇关于正确实施异步任务DbOperations的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!