美文网首页
Android数据库之Realm

Android数据库之Realm

作者: ywzheng | 来源:发表于2016-07-30 00:35 被阅读62次
    • 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();

    相关文章

      网友评论

          本文标题:Android数据库之Realm

          本文链接:https://www.haomeiwen.com/subject/dnozjttx.html