分野

2017-04-27 12 views
2
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)?分野

答えて

0

このソリューションをお試しください。これにより、Realmデータベースが削除されます。

public static boolean deleteRealm(RealmConfiguration configuration) 

これは、レルム内の関数であるコメント欄に言及した例外のためdocs

+0

は、実際に私はアプリレルム多くの場所を使用していたし、すべてが、まだ、この例外を閉じるしようとした)すべてのインスタンスは例外が表示されていると私は(onDestroyでほぼすべての分野のインスタンスを閉じて終了する必要があり、既に試しました –

+0

が同じ例外処理を使用しています。次の回答にコードを投稿します。 –

+0

私の場合、私はこのメソッドrealm.deleteAll()を使用しますが、上記の答えと同じでなければなりません。しかし、私はrealm onStartを開いて、onアクティビティやフラグメントを閉じ、バックグラウンドスレッドですぐに閉じます。 – vsatkh

0

から:

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

まず近いすべてのレルムのインスタンスと、その後

public static void removeAllData(Realm realm) 
{ 
    try { 
     realm.close(); 
     Realm.deleteRealm(realm.getConfiguration()); 
    } catch (Exception e) { 
     Log.e(TAG, "removeAllData:" + e.getMessage()); 
    } 
} 
をdeleteRealm呼び出します

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

あなたの全体のレルム(スキーマ)を削除する正しい方法を使用することです:

Realm realm = Realm.getDefaultInstance(); 
realm.beginTransaction(); 

// delete all realm objects 
realm.deleteAll(); 

//commit realm changes 
realm.commitTransaction(); 

これはRealmObjectクラスを拡張し、すべてのレルムのオブジェクトを削除することを、注意してください。

Original answer here

関連する問題