LoginSignup
16
10

More than 5 years have passed since last update.

【Realm】オブジェクトファイルを更新したらMigrationとスキーマファイルのバージョン設定が必要

Posted at

Realmファイルをつくったけど、やっぱりもう一回オブジェクトファイルの内容を更新!
image
っと、ちょっと待ってください!!

このままでは、Realmをしようとしたときにエラーが出てしまいます。
image

どうやら、マイグレーションが必要であるのと、スキーマファイルのバージョン設定が必要のようです。

Objectファイルを変更したら、とりあえず以下のコードをAppDelegateに追加しましょう。

AppDelegate.swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // ここから追加
        let config = Realm.Configuration(
            schemaVersion: 1,
            migrationBlock: { migration, oldSchemaVersion in
                if (oldSchemaVersion < 1) {}
        })
        Realm.Configuration.defaultConfiguration = config
        // ここまで

        return true
    }

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