ORMLite應用之去Annotation化,ormliteannotation

來源:互聯網
上載者:User

ORMLite應用之去Annotation化,ormliteannotation

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倍~待我驗證一下~做個對比實驗。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.