ORMLite应用之去Annotation化
来源:互联网 发布:网络信息点测试 编辑:程序博客网 时间:2024/06/04 18:35
ORMLite的官方文档上,有这么一段话:
Although improvements and DAO caching has been made, creating a couple of DAOs when your application starts can still take too long and generate far too much garbage collection activity. Turns out that one of the major culprits is some ugly code down in the Android OS – especially in Method.equals(). Because annotations use this method, looking up annotation values is extremely expensive, often garbage collecting thousands of objects and megabytes of space. Android knows about the issues and a fix has been made but we have no idea when these performance improvements will make it into an Android release.
大致意思就是说,查找annotation在Android平台上非常耗时间(extremely expensive)
在去Annotation上,ORMLite也做了一些尝试。
利用Java的反射(Reflection)方法,读取配置文件。
Step 1:
Java 实体类
package com.baidu.ormperformancetest.ormlite;import com.j256.ormlite.field.DatabaseField;import com.j256.ormlite.table.DatabaseTable;@DatabaseTable(tableName = "simpledata")public class SimpleData {@DatabaseField(id = true, generatedId = true)private long id;@DatabaseField(canBeNull = false)private String name;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
Step 2:
继承OrmLiteConfigUtil.java类,注意:这个类是在本地开发环境下执行的,不是在Android平台上。
package com.baidu.ormperformancetest.ormlite;import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;public class DatabaseConfigUtil extends OrmLiteConfigUtil {public static void main(String[] args) throws Exception {writeConfigFile("ormlite_config.txt");}}执行的时候注意:
1、
JRE要选择5或者6,如下图:
2、
Classpath选项卡下,Bootstrap Entries的Android选项一定要Remove掉~
Step 3:
完成上述步骤后,直接Run之~在Console窗口出现如下图所示语句表示执行成功:
Step 4:
完成上述操作后,即可把所有的Annotation去掉了。
Step 5:
继承OrmLiteSqliteOpenHelper.java 类
package com.baidu.ormperformancetest.ormlite;import java.sql.SQLException;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import com.baidu.ormperformancetest.R;import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;import com.j256.ormlite.support.ConnectionSource;import com.j256.ormlite.table.TableUtils;public class ORMLiteDBHelper extends OrmLiteSqliteOpenHelper {private static final String DB_NAME = "test-db";private static final int DB_VERSION = 1;public ORMLiteDBHelper(Context context) {super(context, DB_NAME, null, DB_VERSION, R.raw.ormlite_config);}@Overridepublic void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {try {TableUtils.createTableIfNotExists(arg1, SimpleData.class);} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,int arg3) {}}
以上。
官方文档说,这种方法比Annotation的方法快20倍~待我验证一下~做个对比试验。
- ORMLite应用之去Annotation化
- Android ORM 框架之 Android中ORMLite应用基础
- ormlite 的简单应用
- android OrmLite 实际应用
- android OrmLite 实际应用
- 数据库之OrmLite
- ormlite学习之记录
- ormlite
- ORMLite
- OrmLite
- Android数据库开发之ORMLite
- Android数据库开发之ORMLite
- Android ORM框架之 ORMLite
- Android之ORMLite使用(1)
- android 数据库使用之OrmLite
- 数组、数组应用之去零
- MapReduce之去重计数类应用
- Oracle之分析函数 去重 应用
- c++ 试题
- Rotate Array (Java)
- Android里merge和include标签的使用
- Fragment 类相关小知识
- CodeForces 204A Little Elephant and Interval
- ORMLite应用之去Annotation化
- 学习笔记:NSScanner类的使用
- 通过url获取图片尺寸的几种方法:JS和php
- 图像语义分析--深度学习方法
- 【入门】计算等腰三角形中的数字的位置
- 留不住的2014
- 【BZOJ3890】【Usaco2015 Jan】Meeting Time 拓扑图简单DP
- TTTAttributedLabel 使用方法
- activity继承Cocos2dxActivity后无法监听安卓返回按键