ハマログ

株式会社イーツー・インフォの社員ブログ

Swift2でRealmの設定値を確認する

Swiftの内部DBにRealmをつかった開発をしていて、既存のテーブル定義を変更した際に以下のエラーが表示されました。

Realm
https://realm.io/jp/

エラー

Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=0 “Migration is required for object type ‘Setting’ due to the following errors:
– Property ‘key’ has been added to latest object model.” UserInfo={NSLocalizedDescription=Migration is required for object type ‘User’ due to the following errors:
– Property ‘key’ has been added to latest object model.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 50
(lldb)

すでにあるモデルに影響があるからマイグレーション(移行作業)を実行してね、というRealmのエラーです。とりあえず開発中なので、実体ファイルを削除すればいいかと思いましたが、実体ファイルの場所がいまいちわかりませんでした。

ちなみに、Swift1.2でつかっていたsetDefaultRealmPathは、2.0で非推奨(deprecated)となったようで、警告が表示されます。
RLMRealm.setDefaultRealmPath(“path.to.name”)

setDefaultRealmPath is deprecated: Use+[RLMRealmConfigration setDefaultConfigration]

Swift2.0からは、Realm.Configurationクラスを利用します。

Realm.Configuration
https://github.com/realm/realm-cocoa/blob/master/RealmSwift-swift2.0/RealmConfiguration.swift

コードをみると、以下のプロパティが存在していたため、それぞれ値を確認してみます。

var config = Realm.Configuration()
print(config.description)
print(config.encryptionKey)
print(config.inMemoryIdentifier)
print(config.migrationBlock)
print(config.objectTypes)
print(config.path)
print(config.readOnly)
print(config.schemaVersion)

// 初期化
let realm = try! Realm(configuration: config)

結果

Realm.Configuration {
path = /Users/tomo/Library/Developer/CoreSimulator/
  Devices/69120A91-5110-4515-AA1C-8D51320124B2/data/
  Containers/Data/Application/4223ABD4-9FFA-12FF-2B36-DC224E46B6EA/
  Documents/default.realm;
	inMemoryIdentifier = (null);
	encryptionKey = (null);
	readOnly = 0;
	schemaVersion = 0;
	migrationBlock = (null);
	dynamic = 0;
	customSchema = (null);
}
nil
nil
nil
nil
Optional("/Users/tomo/Library/Developer/CoreSimulator/
  Devices/6A650C91-5660-4585-BB1C-8D5D210124B2/data/Containers/
  Data/Application/472305D4-9E66-49FF-8A36-15224E46B6EA/
  Documents/default.realm")
false
0

表示されたパスを確認すると、ファイルが見つかりました

Screen Shot 2015-10-11 at 18.36.32

cocoaiOSRealmSwiftSwift2.0

  kaneko tomo   2015年10月11日


関連記事

天気頭痛とうまく付き合いたい

こんにちは!最近はめっきり秋めいてきましたね。 何年か前から謎の頭痛に悩まされて…

Laravel5事始め

Laravel4からLaravel5へ。 会社でのPHPスクラッチ開発ではLar…

ITツールの機能紹介-zoomのブレイクアウトルーム-

つーじーです。 コロナ禍がもう1年以上も続いて、特にIT系の会社ではリモート勤務…


← 前の投稿

次の投稿 →