在Android中加入和使用Realm(速度更快,更先進加密性更好)

介紹

如果你比較關心android開發的最新動態的話,那麼你肯定會聽說過Realm,Realm是一個輕量級的數據庫,在Android開發中,它可以替代SQLiteORM框架。

和SQLite相比,Realm速度更快並且它有很多先進的特性,例如對JSON的支持,流暢的API,數據變化通知(觀察者),加密支持… 所有的這一切都會讓android開發者日子過得更瀟灑(這裏扯淡了,開發者日子怎麼可能瀟灑,當然也可能是國內外不同…)。

這篇文章的主題就是Realm for Android, 在這篇文章中,我將使用Realm v0.84.1

將Realm添加到項目

要在Android項目中使用Realm, 你需要在module的build.gradle文件中添加如下代碼:

compile 'io.realm:realm-android:0.84.1'

創建一個Realm

一個Realm和SQLite比較類似。它有一個文件和他作爲關聯,這個文件一次創建,就會永久存在Android系統中。

想要創建一個Realm, 你可以在任意一個Activity中調用靜態方法Realm.getInstance

Realm myRealm = Realm.getInstance(context);

需要注意的是,我們這樣創建,沒有給它指定RealmConfiguration,這時候創建的文件會使用默認的文件名default.realm。 
如果你需要創建另外一個Realm,那你就必須要給它傳遞RealmConfiguration.Builder了,並且要給它指定一個唯一的名稱。

Realm myOtherRealm =
        Realm.getInstance(
                new RealmConfiguration.Builder(context)
                        .name("myOtherRealm.realm")
                        .build()
);

創建RealmObject

如果一個javabean繼承了RealmObject,那麼它就可以用來存儲Realm,如果你想知道什麼是javabean,來看看這裏的定義:javabean是可序列化的,有一個默認的構造方法,成員變量都提供getter和setter方法。例如,下面的代碼可以輕鬆的保存到Realm中,

public class Country extends RealmObject {

    private String name;
    private int population;

    public Country() { }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

}

可以使用@PrimaryKey註解來表示這個一個成員變量是Realm的主鍵。例如下面的代碼是將code字段作爲主鍵,

@PrimaryKey
private String code;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

創建事務

你將會在下面的代碼中發現,使用Realm查詢數據非常簡單, 寫入數據稍稍麻煩點。Realm遵循ACID以保證操作的原子性和一致性。在Realm中,所有的寫操作都會在一個事務中。

使用beginTransaction方法可以開啓一個事務,使用commitTransaction方法去提交一個事務。下面的代碼表明如何創建並保存一個Country

myRealm.beginTransaction();

// Create an object
Country country1 = myRealm.createObject(Country.class); 

// Set its fields
country1.setName("Norway");
country1.setPopulation(5165800);
country1.setCode("NO");

myRealm.commitTransaction();

你可能已經注意到了,country1對象並不是使用Country的構造方法創建的,想讓RealmObject保存這個實例, 就必須使用createObject方法創建。

如果你必須要使用構造方法的話,別忘了在提交事務之前調用copyToRealm方法將對象關聯到Realm上,例如:

// Create the object
Country country2 = new Country();
country2.setName("Russia");
country2.setPopulation(146430430);
country2.setCode("RU");

myRealm.beginTransaction();
    Country copyOfCountry2 = myRealm.copyToRealm(country2);
myRealm.commitTransaction();

查詢

Realm提供了一套非常直觀簡單的API用來查詢操作,調用Realmwhere方法,並且將你需要的類的class傳遞進去, 你就完成了一個查詢的創建,之後, 你可以調用findAll方法來遍歷所有的數據,返回的數據保存在RealmResults對象中,下面的例子,我們查詢出來並且打印了所有的Country

RealmResults<Country> results1 =
        myRealm.where(Country.class).findAll();

for(Country c:results1) {
    Log.d("results1", c.getName());
}

// Prints Norway, RussiaRealmResults<Country> results1 =
        myRealm.where(Country.class).findAll();

for(Country c:results1) {
    Log.d("results1", c.getName());
}

// Prints Norway, Russia

除此之外, 你還可以使用beginsWithendsWithlesserThangreaterThan方法來過濾結果,下面的例如展示瞭如何使用greaterThan方法來查詢所有的population大於1億的Country

RealmResults<Country> results2 =
        myRealm.where(Country.class)
                .greaterThan("population", 100000000)
                .findAll();

// Gets only Russia

如果你想讓查詢結果按照某個順序來,你可以使用findAllSorted方法,它有一個String類型的參數和一個boolean類型的參數,其中,String指定用來排序的字段,boolean指定了排序方式,

// Sort by name, 降序排列
RealmResults<Country> results3 =
        myRealm.where(Country.class)
                .findAllSorted("name", false);

// Gets Russia, Norway

想要學習更多關於Realm的知識,可以參考官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章