简体   繁体   中英

realm android delete all data

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

When i logout from app i need to clear data of realm for that i have to clear every class like this so is there any way to delete all data of realm without having to write all this mRealm.clear(ClassName.class) for every structure?

The right way of deleting your entire Realm (schema) is to use :

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

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

//commit realm changes
realm.commitTransaction();

Please be aware, that this will delete all realm object that extends RealmObject class.

Original answer here

For updated realm transaction format

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

Try this solution. This will delete your Realm database.

public static boolean deleteRealm(RealmConfiguration configuration)

This is a function in Realm, from the docs

First close all realm instances and then call 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);
    }

realm.deleteAll() will do all the magic but

Never use beginTransaction() unless you want to commitTransaction() transaction manually for some reason

Best way is to use executeTransaction() because it will commitTransaction even though if any exception raised inside transaction

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

If you really want to use Realm in an efficient and easy way then read This Article . I personally found it alot useful than official documentation

for your mentioned exception in comments:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM