-
Realm
-
初始化
// for realm
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration); -
增
mRealm = Realm.getDefaultInstance();
mRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
ArticleSave articleSave = bgRealm.createObject(ArticleSave.class);
articleSave.setDetail(mArticle.getDetail());
articleSave.setTitle(mArticle.getTitle());
articleSave.setIntr(mArticle.getIntr());
articleSave.setImg(mArticle.getImg());
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Snackbar.with(getApplicationContext())
.text(getString(R.string.save_ok))
.show(ArtDetailActivity.this);
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Snackbar.with(getApplicationContext())
.text(getString(R.string.save_error))
.show(ArtDetailActivity.this);
}
}); -
删
mRealm = Realm.getDefaultInstance();
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<ArticleSave> results = realm.where(ArticleSave.class)
.equalTo("title",mSaveAdapter.getItem(position).getTitle())
.findAll();
results.deleteFirstFromRealm();
Log.d(TAG,"delete success");
}
}); -
改
-
查
mRealm = Realm.getDefaultInstance();
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmQuery<ArticleSave> query = realm.where(ArticleSave.class);
RealmResults<ArticleSave> result1 = query.findAll();
fillData(result1);
}
}); -
关闭数据库
if(mRealm != null)
mRealm.close();
网友评论