【问题标题】:realm android delete all data领域android删除所有数据
【发布时间】:2017-09-24 22:24:12
【问题描述】:
mRealm.beginTransaction();
mRealm.clear(AboutItemRealm.class);
mRealm.clear(AgendaItemRealm.class);
mRealm.clear(AttendeesItemRealm.class);
mRealm.clear(DocumentsItemRealm.class);
mRealm.clear(FAQsItemRealm.class);
mRealm.clear(GalleryItemRealm.class);
mRealm.clear(GoodToKnowItemRealm.class);
mRealm.clear(MultiEventItemRealm.class);
mRealm.clear(ReservationItemRealm.class);
mRealm.clear(SingleEventItemRealm.class);
mRealm.clear(SpeakerItemRealm.class);
mRealm.commitTransaction();
mRealm.close();

当我从应用程序中注销时,我需要清除领域的数据,因为我必须像这样清除每个类,所以有没有办法删除领域的所有数据而无需编写所有这些 mRealm.clear(ClassName.class)每个结构?

【问题讨论】:

    标签: android realm-mobile-platform


    【解决方案1】:

    试试这个解决方案。这将删除您的 Realm 数据库。

    public static boolean deleteRealm(RealmConfiguration configuration)
    

    这是Realm中的一个函数,来自docs

    【讨论】:

    • 已经试过所有实例都应该关闭异常显示并且我已经关闭了 onDestroy() 中的几乎所有领域实例实际上我已经在应用程序中的许多地方使用了领域并尝试关闭所有但仍然是这个例外显示
    • 同样使用异常处理。在下一个答案中发布代码。
    • 就我而言,我使用这个方法 realm.deleteAll() 但它应该与上面的答案相同。但是我在活动或片段中打开领域 onStart 并关闭 onStop 并立即在后台线程中关闭。
    【解决方案2】:

    对于您在 cmets 中提到的异常:

    try (Realm realm = Realm.getInstance(realmConfig)) {
        realm.beginTransaction();
        //your operations here
        realm.commitTransaction();
    } catch (Exception e) {
        realm.cancelTransaction();
    }
    

    【讨论】:

      【解决方案3】:

      先关闭所有realm实例,然后调用deleteRealm

      public static void removeAllData(Realm realm)
      {
          try {
              realm.close();
              Realm.deleteRealm(realm.getConfiguration());
          } catch (Exception e) {
              Log.e(TAG, "removeAllData:" + e.getMessage());
          }
      }
      

      Realm.Java

      /**
           * Deletes the Realm file specified by the given {@link RealmConfiguration} from the filesystem.
           * All Realm instances must be closed before calling this method.
           *
           * @param configuration a {@link RealmConfiguration}.
           * @return {@code false} if a file could not be deleted. The failing file will be logged.
           * @throws IllegalStateException if not all realm instances are closed.
           */
          public static boolean deleteRealm(RealmConfiguration configuration) {
              return BaseRealm.deleteRealm(configuration);
          }
      

      【讨论】:

      • 这就是答案!
      【解决方案4】:

      删除整个领域(模式)的正确方法是使用:

      Realm realm = Realm.getDefaultInstance();
      realm.beginTransaction();
      
      // delete all realm objects
      realm.deleteAll();
      
      //commit realm changes
      realm.commitTransaction();
      

      请注意,这将删除所有扩展 RealmObject 类的领域对象。

      Original answer here

      更新领域交易格式

      realm.executeTransaction(new Realm.Transaction() {
              @Override
              public void execute(Realm realm) {
                  realm.deleteAll();
              }
          });
      

      【讨论】:

        【解决方案5】:

        realm.deleteAll() 会做所有的魔法,但是

        除非您愿意,否则切勿使用beginTransaction() commitTransaction() 出于某种原因手动交易

        最好的方法是使用executeTransaction(),因为它会commitTransaction,即使在事务中引发任何异常

                try {
                    realm.executeTransaction { realm ->
                        realm.deleteAll()
                    }
                } finally {
                    realm?.close()
                }
        

        如果您真的想以一种高效且简单的方式使用 Realm,请阅读This Article。我个人觉得它比官方文档有用很多

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-11-09
          • 1970-01-01
          • 1970-01-01
          • 2016-09-24
          • 1970-01-01
          • 1970-01-01
          • 2014-11-28
          相关资源
          最近更新 更多