[iOS/Swift] Realm migration 렘 마이그레이션

chaentopia·2023년 2월 10일
0

Realm 라이브러리를 통해서 튜토리얼을 따라하던 중.. model안에 longitude라고 적어야 했던 프로퍼티를 longititude라고 적었던 것을 발견해서 수정했다

그러자 갑자기 에러가 뜨는데..

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error
Domain=jo.realm Code=10 "Migration is required due to the following errors:
- Property 'Specimen. longititude' has been removed.
- Property 'Specimen. longitude' has been added."
UserInfo=(NSLocalizedDescription=Migration is required due to the following errors:
- Property 'Specimen.longititude' has been removed.
- Property 'Specimen.longitude' has been added., Error Code=10}

대충 보면

너 지금 프로퍼티 수정함???? 응 너 마이그레이션 해

라고 하네요

찾아보니까 시뮬레이터를 쓰면 내부에서 앱을 삭제하고 다시 깔면 되긴 하는데 배포 후에는 그게 불가능하니 코드로 해결하는 방법도 있었다.

let config = Realm.Configuration(
                  schemaVersion: 2, 
                  migrationBlock: { migration, oldSchemaVersion in
                      if oldSchemaVersion < 2 {
                          migration.enumerateObjects(ofType: Specimen.className()) { oldObject, newObject in
                              newObject!["longitude"] = 0.0 // 내가 수정한 부분
                          }
                      }
                  }
              )
              Realm.Configuration.defaultConfiguration = config

AppDelegate 안에 위 코드를 집어넣으면 realm에게 스키마 처리방법을 알려주면서 자동으로 마이그레이션을 실행하도록 할 수 있다고 한다.

결과적으로 다시 빌드하면 잘 처리됨.. ^^


참고자료
https://jerry-bakery.tistory.com/entry/iOS-iOS-Realm-Migration%EB%A7%88%EC%9D%B4%EA%B7%B8%EB%A0%88%EC%9D%B4%EC%85%98-%ED%95%98%EB%8A%94%EB%B2%95

profile
the pale blue dot

0개의 댓글