gpt4 book ai didi

android - Realm 配置管理

转载 作者:行者123 更新时间:2023-11-30 01:10:41 29 4
gpt4 key购买 nike

我在我的 android 项目中使用 Realm。目前我在我的应用程序类中定义默认 Realm 配置如下-

 @Override
public void onCreate(){
super.onCreate();
myApplication=this;
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).deleteRealmIfMigrationNeeded().build();
Realm.setDefaultConfiguration(realmConfiguration);
Thread.setDefaultUncaughtExceptionHandler(new UnhandledExceptionHandler());

}

在当前情况下,只有 1 个用户使用设备。她使用她的凭据登录应用程序并使用默认 Realm 配置。

但是随着新的要求,不同的用户可以使用相同的 android 设备,因此我需要为每个用户提供不同的 Realm 配置,以便每个用户都有自己的 Realm 文件,对吗?

如果这是真的,那么管理 Realm 配置的最佳方式是什么。我应该在我的登录 Activity 中这样做吗?然后我应该为登录 Activity 中的每个用户创建不同的 Realm 配置吗?

谢谢

阿普尔瓦

最佳答案

IMO,使用工厂类会很有用,因为您要管理多个 Realm 实例。可能看起来像这样,

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

关于android - Realm 配置管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443688/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com