Skip to content Skip to sidebar Skip to footer

Realm Configuration Management

I am using Realm with my android project. Currently I am defining the default realm configuration in my application class as follows- @Override public void onCreate(){ super.o

Solution 1:

IMO, using a factory class would be useful, since you are managing multiple Realm instances. which may look like this,

public class RealmFactory {
/* Realm
 * CAUTION: Be careful which thread you call this from, it is not Thread safe */
public static Realm getRealmInstance(Context context, String primaryKeyFromUser) {
    return Realm.getInstance(getRealmConfiguration(context, primaryKeyFromUser));
}

/* RealmConfiguration */
private static RealmConfiguration getRealmConfiguration(Context context, String primaryKeyFromUser) {
    return new RealmConfiguration.Builder(context)
            .name(primaryKeyFromUser)
            .encryptionKey(getSecurityKey())
            .deleteRealmIfMigrationNeeded()
            .build();
}

/* SecurityKey, 
 * CAUTION: This is just a sample */
private static byte[] getSecurityKey() {
    char[] chars = "16CharacterLongPasswordKey4Realm".toCharArray();
    byte[] key = new byte[chars.length * 2];
    for (int i = 0; i < chars.length; i++) {
        key[i * 2] = (byte) (chars[i] >> 8);
        key[i * 2 + 1] = (byte) chars[i];
    }

    return key;
}

/* Check for Realm file */
public static boolean isRealmFileExists(Context context, String primaryKeyFromUser) {
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context)
            .name(primaryKeyFromUser)
            .encryptionKey(getSecurityKey())
            .deleteRealmIfMigrationNeeded()
            .build();

    File realmFile = new File(realmConfiguration.getPath());
    return realmFile.exists();
}

/* Delete Realm file, if exists
 * CAUTION: if the Realm instance with given configuration is open, make sure you close it
 *          first, before deleting it, else an Exception will be thrown. */
public static void deleteRealmFile(Context context, String primaryKeyFromUser) {
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context)
            .name(primaryKeyFromUser)
            .build();
    Realm.deleteRealm(realmConfiguration);
}

/* Delete all Realm files, if exists
 * CAUTION: if the Realm instance with given configuration is open, make sure you close it
 *          first, before deleting it, else an Exception will be thrown. */
public static void deleteAllRealmFiles(Context context) {
    File file = new File(context.getFilesDir().getPath());

    File list[] = file.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    });

    for (File deleteFile : list) {
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context)
                .name(deleteFile.getName())
                .build();
        Realm.deleteRealm(realmConfiguration);
    }
}
}

Post a Comment for "Realm Configuration Management"