问题
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.onCreate();
myApplication=this;
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).deleteRealmIfMigrationNeeded().build();
Realm.setDefaultConfiguration(realmConfiguration);
Thread.setDefaultUncaughtExceptionHandler(new UnhandledExceptionHandler());
}
In current scenario only 1 user uses a device. She logs in to the app using her credentials and uses default realm configuration.
However with new requirements, the same android device can be used by different users and hence I need different realm configurations for each user so that each user has their own realm file right?
If this is true then what is the best way to manage realm configurations. Should I do that in my login activity? Should I then create different realm configurations for each user in login activity?
Thanks
Apurva
回答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);
}
}
}
来源:https://stackoverflow.com/questions/38443688/realm-configuration-management