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;
    }
}

初始化配置

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;
    }
}

初始化和关闭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();
    }

}

使用


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();
    }

codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...