Realm 是一个orm数据库,适用于android,ios;据说速度超越sqlite(这一点我将在后面有实例验证),还支持json,加密支持
第一步:
compile 'io.realm:realm-android:0.84.1'
第二步:
创建模型:javaBean 必须标准化,字段要私有,要有get set方法,toString方法都不可以有,有严格要求,果真是牺牲自由行来换取速度
package com.xuan.realm.bean; import io.realm.RealmObject; /** * @author xuanyouwu * @email [email protected] * @time 2016-05-05 10:02 */ public class User extends RealmObject { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
第三步:创建一个Realm对象
mRealm = Realm.getInstance(this);
保存对象:
User user = new User(); user.setName("hhh"); user.setAge(25); mRealm.beginTransaction(); mRealm.copyToRealm(user); mRealm.commitTransaction();
RealmResults<User> all = mRealm.where(User.class).findAll();
package com.xuan.realm; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.xuan.realm.bean.User; import com.xuan.realm.utils.LogUtils; import io.realm.Realm; import io.realm.RealmResults; public class MainActivity extends AppCompatActivity { Realm mRealm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRealm = Realm.getInstance(this); } public void onSave(View v) { User user = new User(); user.setName("hhh"); user.setAge(25); mRealm.beginTransaction(); mRealm.copyToRealm(user); mRealm.commitTransaction(); } public void onQuery(View v) { RealmResults<User> all = mRealm.where(User.class).findAll(); LogUtils.d("----->query:" + all); } }
</pre><pre code_snippet_id="1672753" snippet_file_name="blog_20160505_4_2130749" name="code" class="java">对比:插入100次:
<pre name="code" class="java"> long startTime = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { studentDao.insertData(new Student("name:" + SystemClock.elapsedRealtime(), new Random().nextInt(100))); } long endTime = System.currentTimeMillis(); Log.d("-------->sqlite 原生:耗时1:", "" + (endTime - startTime)); long startTime1 = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { Student1 student1 = new Student1(); student1.setName("name:" + SystemClock.elapsedRealtime()); student1.setAge(new Random().nextInt(100)); daoSession.insertOrReplace(student1); } long endTime1 = System.currentTimeMillis(); Log.d("-------->greendao 耗时2:", "" + (endTime1 - startTime1)); long startTime2 = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { StudentReleam student1 = new StudentReleam(); student1.setName("name:" + SystemClock.elapsedRealtime()); student1.setAge(new Random().nextInt(100)); mRealm.beginTransaction(); mRealm.copyToRealm(student1); mRealm.commitTransaction(); } long endTime2 = System.currentTimeMillis(); Log.d("-------->Realm 耗时3:", "" + (endTime2 - startTime2));
05-05 03:32:49.561 11993-11993/com.xuan.greendaotest D/-------->sqlite 原生:耗时1:: 605 05-05 03:32:50.246 11993-11993/com.xuan.greendaotest D/-------->greendao 耗时2:: 685 05-05 03:32:50.677 11993-11993/com.xuan.greendaotest D/-------->Realm 耗时3:: 431
查询100次 总时间:
//查询100次 int totalTime = 0; for (int i = 0; i < 100; i++) { long startTime = System.currentTimeMillis(); String sql = String.format("select sum(%s) from %s", "age", "student"); Cursor cursor = studentDao.helper.getWritableDatabase().rawQuery(sql, null); int sumAge = 0; if (cursor != null && cursor.moveToFirst()) { sumAge = cursor.getInt(0); } long endTime = System.currentTimeMillis(); totalTime += (endTime - startTime); } Log.d("-------->sqlite 原生:耗时1:", "" + totalTime); int totalTime1 = 0; for (int j = 0; j < 100; j++) { long startTime1 = System.currentTimeMillis(); /* String sql = String.format("select sum(%s) from %s", "AGE", "STUDENT1"); Cursor cursor = daoSession.getDatabase().rawQuery(sql, null); int sumAge1 = 0; if (cursor != null && cursor.moveToFirst()) { sumAge1 = cursor.getInt(0); }*/ List<Student1> student1s = daoSession.getStudent1Dao().loadAll(); int sumAge1 = 0; if (student1s != null) { for (int i = 0; i < student1s.size(); i++) { Student1 student1 = student1s.get(i); if (student1 == null) continue; sumAge1 += student1.getAge(); } } long endTime1 = System.currentTimeMillis(); totalTime1 += (endTime1 - startTime1); } Log.d("------->greendao 耗时2:", "" + totalTime1); int totalTime2 = 0; for (int j = 0; j < 100; j++) { long startTime2 = System.currentTimeMillis(); int sumAge2 = 0; RealmResults<StudentReleam> all = mRealm.where(StudentReleam.class).findAll(); if (all != null) { for (int i = 0; i < 100; i++) { StudentReleam student1 = all.get(i); if (student1 == null) continue; sumAge2 += student1.getAge(); } } long endTime2 = System.currentTimeMillis(); totalTime2 += (endTime2 - startTime2); } Log.d("------->realm 耗时3:", "" + totalTime2);
结果:
05-05 03:36:06.226 11993-11993/com.xuan.greendaotest D/-------->sqlite 原生:耗时1:: 1052
05-05 03:36:06.769 11993-11993/com.xuan.greendaotest D/------->greendao 耗时2:: 543
05-05 03:36:07.977 11993-11993/com.xuan.greendaotest D/------->realm 耗时3:: 1208
总结:插入速度 realm>sqlite>greendao
查询速度:sqlite约等于greendao>realm
sqlite 有时候不稳定,几乎和greendao 持平,realm耗时比较稳定,但是几乎是前者的2倍时间