问题描述
我正在将Realm与我的android项目一起使用.当前,我在应用程序类中定义默认领域配置,如下所示-
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());
}
在当前情况下,只有1个用户使用设备.她使用自己的凭据登录到应用程序,并使用默认领域配置.
In current scenario only 1 user uses a device. She logs in to the app using her credentials and uses default realm configuration.
但是有新的要求,同一台android设备可以由不同的用户使用,因此我需要为每个用户使用不同的领域配置,以便每个用户都有自己的领域文件?
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?
谢谢
阿普瓦(Apurva)
Apurva
推荐答案
IMO,使用工厂类将很有用,因为您正在管理多个Realm实例.可能看起来像这样,
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);
}
}
}
这篇关于领域配置管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!