LoginSignup
7
9

More than 5 years have passed since last update.

Realm on Android で開発中にMigrateが面倒な場合はRealmファイルを消してお茶を濁す

Posted at

リリース前しか使えないと思いますけど。

みなさんAndroidでRealm使ってますか?僕は使ってます。
便利なんですが、Schema定義を変えるときに少し困ったので、開発中にクイックにSchemaを入れ替える方法をメモついでに紹介します。

Realm いいよね

Realmはモバイル向けのいわゆるNoSQLというやつで、どうやら速いらしいものです。詳しくは@wasabeef_jpさんのこちらの記事がとても参考になります。
シンプルでちょっとしたアプリには十分な機能を持ってるので、結構いいなと感じています(個人的にRDB/SQLがあまり好きではないのもあります)。

しかし、Migrationがめんどくさい

参考記事にもありますとおり、なんといってもMigrationが面倒くさい。

本家のMigrationの解説](http://realm.io/docs/java/latest/#migrations)を見る限り、まだまだUnder Constructingなようですね(2015/5/2)。

Migrations are a work in progress. The feature is fully functional, but the current interface is cumbersome, and will be rewritten soon.

本家のサンプルを見ると、Currentバージョンをもとにスキーマをどう変えるかを丁寧に記述するしかないようです。

開発中だったら、ファイルごと消しちゃえばいいよね

SQLiteでも同じかもしれませんが、開発中で比較的頻繁にスキーマ定義を変えるような場合、ごっそりデータファイルを消してしまえばいいです。
公式もそう言ってます。

If there is no file on disk when Realm launches, no migration is needed, and Realm will just create a new .realm file & schema based on the latest models defined in your code. This means that if you are in the middle of development and changing your schema very often, and you are OK with losing all your data, you can delete your .realm file on disk (and the entire dataset it contained!) instead of having to write a migration. This can be helpful when tinkering with models early in the development cycle of your app.

当然といえば当然ですが既存データはすべて消えますので注意してください。

やり方

例によって、adb shell

cd C:<path to sdk platform-tools>
adb shell

adb内で、アプリのディレクトリへ

$ cd /data/data/<パッケージ名>

よく忘れるrun-as

$ run-as <パッケージ名>

ここでlsするとRealmのファイルが見えます

$ ls
default.realm
default.realm.lock

普通にrmで消す

念のためもう一度、既存データはすべて消えますので注意してください。

$ rm *.realm*

これで、次回デバッグ時にはまっさらなところから新しいSchemaが作られます。

最後まで見ていただきありがとうございました。

May a Happy Realm Life be with you.

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9