Replacing sqlite with realm

  android

domain

public class Known extends RealmObject{

    @PrimaryKey
    private String id;

    private String title;

    private String content;

    private Date createTime;

    private String tag;

    public Known() {
    }

    public Known(String id, String title, String content, Date createTime) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.createTime = createTime;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

Initialization configuration

public class AppContext extends Application{

    private static AppContext applicationContext;

    private static RealmConfiguration realmConfiguration;

    public static AppContext getInstance(){
        return applicationContext;
    }

    public static boolean selectDrawer = false;

    public static Date selectDate = null;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        realmConfiguration = new RealmConfiguration
                .Builder(this)
                .name("known")//如果你想設置個別資料庫名稱
//                .encryptionKey(key)//如果你需要加密的話
                .build();
    }

    public static RealmConfiguration getRealmConfig(){
        return realmConfiguration;
    }
}

Initialize and close realm

public class MainActivity extends AppCompatActivity {

private Realm realm;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        realm = Realm.getInstance(AppContext.getRealmConfig());
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }

}

Use


public List<Known> findAll(Realm realm){
        return realm.where(Known.class).findAll();
    }

    public void insert(Known known,Realm realm){
        known.setId(UUID.randomUUID().toString());
        realm.beginTransaction();
        realm.copyToRealm(known);
        realm.commitTransaction();
    }

    public void update(final Known known,Realm realm){
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(known);
        realm.commitTransaction();
    }

    public void delete(String id,Realm realm){
        RealmResults<Known> results = realm.where(Known.class).equalTo("id", id).findAll();
        realm.beginTransaction();
        results.clear();
        realm.commitTransaction();
    }