Created
May 8, 2018 06:10
-
-
Save kiuchikeisuke/39f64738741e15c5911523296afc53e9 to your computer and use it in GitHub Desktop.
RealmのinitalDataでハマったので覚書 ref: https://qiita.com/k_keisuke/items/24e8a5bbf69a5c963945
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides | |
fun provideRealm(application: MainApplication): Realm { | |
val builder = RealmConfiguration.Builder().name(APP_KEY) | |
builder.initialData { /*引き継ぎ処理*/ } //追加 | |
return if (BuildConfig.DEBUG) { | |
Realm.getInstance(builder.deleteRealmIfMigrationNeeded().build()) | |
} else { | |
Realm.getInstance(builder.build()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.RuntimeException: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. | |
Cached configuration: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides | |
fun provideRealm(transaction: Realm.Transaction): Realm { | |
val builder = RealmConfiguration.Builder().name(APP_KEY) | |
builder.initialData(transaction) | |
return if (BuildConfig.DEBUG) { | |
Realm.getInstance(builder.deleteRealmIfMigrationNeeded().build()) | |
} else { | |
Realm.getInstance(builder.build()) | |
} | |
} | |
@Provides | |
fun provideTransaction(application: MainApplication) = application.transaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides | |
fun provideRealm(application: MainApplication): Realm { | |
val builder = RealmConfiguration.Builder().name(APP_KEY) | |
builder.initialData { /*初期化処理*/ } //ラムダ式なので毎回異なるインスタンス | |
return if (BuildConfig.DEBUG) { | |
Realm.getInstance(builder.deleteRealmIfMigrationNeeded().build()) | |
} else { | |
Realm.getInstance(builder.build()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainApplication : DaggerApplication() { | |
val transaction: Realm.Transaction by lazy { | |
Realm.Transaction { /* 初期化処理 */} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void validateConfiguration(RealmConfiguration newConfiguration) { | |
if (configuration.equals(newConfiguration)) { | |
// Same configuration objects. | |
return; | |
} | |
// 中略 | |
throw new IllegalArgumentException("Configurations cannot be different if used to open the same file. " + | |
"\nCached configuration: \n" + configuration + | |
"\n\nNew configuration: \n" + newConfiguration); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { return true; } | |
if (obj == null || getClass() != obj.getClass()) { return false; } | |
RealmConfiguration that = (RealmConfiguration) obj; | |
if (schemaVersion != that.schemaVersion) { return false; } | |
if (deleteRealmIfMigrationNeeded != that.deleteRealmIfMigrationNeeded) { return false; } | |
if (readOnly != that.readOnly) { return false; } | |
if (isRecoveryConfiguration != that.isRecoveryConfiguration) { return false; } | |
if (realmDirectory != null ? !realmDirectory.equals(that.realmDirectory) : that.realmDirectory != null) { | |
return false; | |
} | |
if (realmFileName != null ? !realmFileName.equals(that.realmFileName) : that.realmFileName != null) { | |
return false; | |
} | |
if (!canonicalPath.equals(that.canonicalPath)) { return false; } | |
if (assetFilePath != null ? !assetFilePath.equals(that.assetFilePath) : that.assetFilePath != null) { | |
return false; | |
} | |
if (!Arrays.equals(key, that.key)) { return false; } | |
if (migration != null ? !migration.equals(that.migration) : that.migration != null) { | |
return false; | |
} | |
if (durability != that.durability) { return false; } | |
if (!schemaMediator.equals(that.schemaMediator)) { return false; } | |
if (rxObservableFactory != null ? !rxObservableFactory.equals(that.rxObservableFactory) : that.rxObservableFactory != null) { | |
return false; | |
} | |
if (initialDataTransaction != null ? !initialDataTransaction.equals(that.initialDataTransaction) : that.initialDataTransaction != null) { | |
return false; | |
} | |
return compactOnLaunch != null ? compactOnLaunch.equals(that.compactOnLaunch) : that.compactOnLaunch == null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment