LoginSignup
10
11

More than 5 years have passed since last update.

Realmのスキームバージョンをビルド番号に合わせる

Last updated at Posted at 2015-07-03

Realmでマイグレーション処理をしないと例外が発生します。
例えば、RLMObjectのサブクラスとして作ったモデルクラスのプロパティを増やしたりすると発生します。
マイグレーションでは、Realmのスキームバージョンを更新することで行うのですがこれがついつい忘れがちです。
リリース時はマイグレーションを厳密に行いますが開発中ではプロパティの変更が頻繁に起きたりします。
一人で開発している場合はrealmのファイルごと消してしまっても良いのですが、TestFlightなどで配布している場合等なるべく現状維持したい場合もあるかと思います。
自動的にマイグレーションさせるのが正しいかというと、不具合の温厚にしかならなかったりするのですが上記の更新忘れ等が防げたり一長一短なのでこんな方法もあるよ程度のエントリです。(すいません)

まずはビルドスクリプトでビルド番号を更新します。

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

AppDelegate.mで起動時にビルド番号をスキームバージョンとして利用します。

- (void)drasticMigration{
    NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSLog(@"Migration done. (%d)",build.intValue);
    [RLMRealm setSchemaVersion:build.integerValue
                forRealmAtPath:[RLMRealm defaultRealmPath]
            withMigrationBlock:nil];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self drasticMigration];
    return YES;
}

以上です。
realm内の情報をmigrationで色々する際は不便になってしまうので注意です

10
11
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
10
11