我有1500多个此类的项目存储在数据库中:
public class Task implements Parcelable{
@DatabaseField(dataType = DataType.LONG, id = true)
private long ActionId;
@DatabaseField(dataType = DataType.STRING)
private String Text;
@DatabaseField(dataType = DataType.STRING)
private String actionText;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isUrgent;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isImportant;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isCompleted;
@DatabaseField(dataType = DataType.STRING)
private String description;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isDeleted;
@DatabaseField(foreign = true, foreignAutoRefresh = true, canBeNull = true, index = true)
private Role role;
@DatabaseField(foreign = true, foreignAutoRefresh = true, canBeNull = true, index = true)
private List list;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isWeekly;
@DatabaseField(dataType = DataType.STRING)
private String Date;
@DatabaseField(dataType = DataType.LONG_OBJ)
Long ActionListId;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long createdAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long updatedAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long deletedAt;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isNew;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long roleId;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long WorkspaceId;
@DatabaseField(dataType = DataType.INTEGER)
private int order;
@DatabaseField(dataType = DataType.STRING)
private String WeekDate;
@Override
public boolean equals(Object o) {
if(o instanceof Task) {
Task task = (Task)o;
if(task.getActionId() == this.getActionId())
return true;
return false;
} else return false;
}
@Override
public String toString() {
return "Name : " + Text;
}
}
这是这两个外国班的代码:
public class Role implements Parcelable{
@DatabaseField(dataType = DataType.LONG, id = true)
long id;
@DatabaseField(dataType = DataType.STRING)
String date;
@DatabaseField(dataType = DataType.STRING)
String Text;
@DatabaseField(dataType = DataType.STRING)
String Color;
@DatabaseField(dataType = DataType.LONG)
long WorkspaceId;
@DatabaseField(dataType = DataType.INTEGER)
int Momentum;
@DatabaseField(dataType = DataType.BOOLEAN)
boolean isDeleted;
@DatabaseField(dataType = DataType.LONG_OBJ)
Long createdAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
Long editedAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
Long deletedAt;
@DatabaseField(dataType = DataType.BOOLEAN)
boolean isNew;
@DatabaseField(dataType = DataType.INTEGER)
private int year;
@DatabaseField(dataType = DataType.INTEGER)
private int month;
@DatabaseField(dataType = DataType.INTEGER)
private int day;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long UserId;
@DatabaseField(dataType = DataType.STRING)
private String UserName;
@DatabaseField(dataType = DataType.INTEGER)
private int order;
}
public class List {
@DatabaseField(dataType = DataType.INTEGER, generatedId = true)
private int id;
@DatabaseField(dataType = DataType.LONG)
private long ActionListId = -1;
@DatabaseField(dataType = DataType.STRING)
private String Name;
@DatabaseField(dataType = DataType.LONG)
private long WorkspaceId;
@DatabaseField(dataType = DataType.STRING)
private String day;
@DatabaseField(dataType = DataType.SERIALIZABLE, canBeNull = true)
private int[] date;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean parkingLot;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long CreatedAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long EditedAt;
@DatabaseField(dataType = DataType.LONG_OBJ)
private Long DeletedAt;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean IsDeleted;
@DatabaseField(dataType = DataType.BOOLEAN)
private boolean isNew;
@DatabaseField(dataType = DataType.INTEGER)
private int taskCount;
@DatabaseField(dataType = DataType.INTEGER)
private int Order;
}
注意:我已经阅读了这些帖子:ORMLite poor performance on Android?,这:Ormlite DAO in android getting really slow when querying more than few thousand results,How can I improve this method of loading objects with ORMLite on Android? 而且我也读到了这个:ORMLite's createOrUpdate seems slow - what is normal speed?,事实上,这1500多个项目的创建很顺利,但这个查询工作得太慢了:
try {
dao = (TaskDao) DatabaseSingleton.getDAO(Task.class);
QueryBuilder<Task, Integer> qB = dao.queryBuilder();
qB.where().eq("WorkspaceId", workspaceId).and().eq("isDeleted", false);
CloseableIterator<Task> iterator = dao.iterator(qB.prepare());
try {
while (iterator.hasNext()) {
Task item = iterator.next();
ActionDto outputItem = new ActionDto(item);
list.add(outputItem);
}
} finally {
iterator.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
所以迭代器在这里没有帮助
另一个注意事项:我读过缓慢的注释,并且它已经在Gingerbread中修复了,所以我想告诉我正在使用哪些设备:
它对所有人来说都很慢,我认为它可能与我课程中的大量LONG_OBJECT以及每个LONG_OBJECT中的字段数量有关,但实际上需要花费很多时间来加载1500多个项目( 持续10秒以上)
而且我真的不想改变ORM,因为除了这种情况,ORMLite对我来说效果很好
修改:由于评论中有回复,我不得不说只有qB.query()
与iterator
循环相同的时间1>