1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Realmを更新しようとしたら"Can't perform transactions on a frozen Realm"が出た

Posted at

事象

Realmを更新しようとしたら以下のエラーが発生してアプリクラッシュした。

Terminating app due to uncaught exception 'RLMException', reason: 'Can't perform transactions on a frozen Realm'

エラーメッセージの意味

Can't perform transactions on a frozen Realm

直訳すると 凍結されたRealmに対してトランザクションを実行できません

読み取り専用オブジェクト(frozen Realm)に対してトランザクションの実行(値書き込み)ができないということ。

Realmのfreezeとは?

筆者自身まだ勉強中なので詳しくわかっていませんが、Realmデータを読み取り専用にする仕組み(だと認識しています)
読み取り専用にすることで、別スレッドから値を上書きされる心配がないのでマルチスレッドアクセスが可能になります。
今回の場合、上書きNGなfreezeしたデータを更新しようとしたためエラーになったという感じです。

freezeはどうやって設定する or 解除する?

freezeは以下のようにすれば明示的に実装できる。
簡単ですね。

let frozenTodo = todo.freeze()

freezeを解除するにはthaw()を使う。


if let todo = frozenTodo {
    // ここでfreezeを明示的に解除し変更可能な状態にする
    if let thawedTodo = todo.thaw() {
        // Realmの上書きも可能になる
        try! realm.write {
            thawedTodo.param1 = "変更可能なので更新します"
        }
    }
}

こまかいことはまだ勉強中です。
今回は問題解決できたので、また同様の事象が発生したらその時にまた更新します。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?