在android中将领域版本从0.82.1更改为0.87.5后致命异常崩溃

时间:2016-03-07 13:06:52

标签: android realm realm-migration

将域的版本从0.82.1更改为0.87.5后,我收到此错误。

FATAL EXCEPTION: main Process: com.xxxxx.consumer, PID: 8633
java.lang.RuntimeException: Unable to create applicationcom.gemba.consumer.realm.AppInstance: 
java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
Cached configuration: 
                                                              realmFolder: /data/user/0/com.xxxx.consumer/files
                                                              realmFileName : default.realm
                                                              canonicalPath: /data/data/com.xxxx.consumer/files/default.realm
                                                              key: [length: 0]
                                                              schemaVersion: 0
                                                              migration: null
                                                              deleteRealmIfMigrationNeeded: true
                                                              durability: FULL
                                                              schemaMediator: io.realm.DefaultRealmModuleMediator@b40b99c9

我的领域经理课就是这个     公共最终类RealmManager {

public static RealmManager realmManager;
public static RealmConfiguration realmConfig;
public static Realm realm;
public static WorkerThread workerThread;
public static Context appContext;

public static RealmManager getInstance(Context context) {

    if (realmManager == null) {
        realmManager = new RealmManager();

        getRealmConfig(context);
        realm = Realm.getInstance(realmConfig);
        workerThread = new WorkerThread(context);
        appContext = context;
    }

    return realmManager;
}

public static RealmConfiguration getRealmConfig(Context context) {
    if (realmConfig == null) {
        realmConfig = new RealmConfiguration.Builder(context)
                .deleteRealmIfMigrationNeeded()
                .build();

    }
    return realmConfig;

}

我在Application类中使用它。

RealmManager.getInstance(getApplicationContext());

1 个答案:

答案 0 :(得分:2)

每个Realm实例都是基于RealmConfiguration

创建的

getInstance()调用RealmConfiguration指向已在另一个RealmConfiugration上打开的Realm文件(同一文件路径)时,新配置必须与旧的。

getInstance(Context context)的实施如下所示。

public static Realm getInstance(Context context) {
    return Realm.getInstance(new RealmConfiguration.Builder(context)
            .name(DEFAULT_REALM_NAME)
            .build());
}

致电时

RealmManager.getInstance(getApplicationContext());

在app的文件目录中隐式创建了RealmConfiguration,其默认的Realm名称为“default.realm。”它与您在getRealmConfig中创建的内容不同 - 没有deleteRealmIfMigrationNeeded()

要解决此问题,请使用getRealmConfig返回的相同配置。顺便说一下,你的getRealmConfig()不是线程安全的,你可能想修复它,如果它将在不同的线程中调用。

在Realm 0.88.0中不推荐使用

Realm.getInstance(Context context)