Realmデータベースはsqliteに取って代わりますか?

8433 ワード

Realmはandroid,iosに適したormデータベースです.速度はsqliteを超え、json、暗号化サポートもサポートされているという.
ステップ1:
compile 'io.realm:realm-android:0.84.1'

ステップ2:
モデルの作成: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;
    }



}

ステップ3: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/com.xuan.greendaotest D/->greendao消費時間2::543 05-05 03:36:07.977 11933-1993/com.xuan.Greendaotest D/---->realm所要時間3::1208
まとめ:挿入速度realm>sqlite>greendao
クエリー速度:sqliteはgreendao>realmにほぼ等しい
sqliteは不安定でgreendaoとほぼ横ばいでrealmの消費時間は比較的安定しているが,ほぼ前者の2倍の時間である.