Android - Realm - 正确使用Realmconfiguration

标签 android realm

今天我要切换到 Realm,我想知道 RealmConfiguration 的确切用途。

假设我有两个使用 Realm 的 Activity 。

在获取 Realm 的 defaultInstance 时,我必须指定 RealmConfiguration。我每次都必须在我的两个 Activity 中调用它吗?它具体做什么?这是我的数据吗?例如,我应该在应用程序类中声明一次吗?

// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.    
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);

我试图在 Realm 上搜索,但找不到答案。

非常感谢您的帮助,

最佳答案

When getting the defaultInstance of Realm, I have to specify the RealmConfiguration. Do I have to call this each time on my two activities?

可以,但不应该。

Should I declare this once in the application class for instance?

是的。

public class MyApp extends Application  {

    @Override
    public void onCreate() {
        super.onCreate();

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);
    }
}

然后您就可以在您的应用中的任何地方说这句话。

Realm realm = Realm.getDefaultInstance();

它将使用您在应用程序类中设置的 RealmConfiguration 进行配置。

What does it do exactly? is this my data?

这不是您的数据,而是您数据的配置。例如。数据库文件的名称是什么,架构是什么版本,数据是否加密,应如何处理迁移到新架构版本等。查看更多选项 here .

如果您的应用中有多个 Realm 文件,您可以使用多个 RealmConfigurations。每人一个。不过,在您的应用中只有一个 Realm 文件是完全正常的。

关于Android - Realm - 正确使用Realmconfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432421/

相关文章:

Android setError() 消息在 ActionBar 顶部滚动

android - 我们建议使用较新的 Android Gradle 插件在 android studio 中使用 compileSdkPreview = "Sv2"警告

ios - "Use of unresolved identifier ' Realm '"

swift - Realm.io API 自动映射父对象

realm - 为什么 Realm 与 SQLite 相比如此快?

android - 如何在 Skobbler map 中使用 SKNavigationManager 将导航模式设置为 3d?

java - 为什么 Html.fromHtml 不适用于 Android 中的 String?

ios - 在我的 swift 项目中添加 Realm 数据库时出现 RLMCollection.h 错误

java - 如何使用 Realm 创建递归关系?

android - WebView 作为动态壁纸?