Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RealmSwift Migration #40

Closed
kimkyuchul opened this issue Sep 6, 2023 · 0 comments
Closed

RealmSwift Migration #40

kimkyuchul opened this issue Sep 6, 2023 · 0 comments
Assignees
Labels

Comments

@kimkyuchul
Copy link
Owner

kimkyuchul commented Sep 6, 2023

import UIKit

import RealmSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let config = Realm.Configuration(schemaVersion: 3) { migration, oldSchemaVersion in
            
            // 단순 컬럼 추가 삭제는 별도 코드가 필요 없다.
            if oldSchemaVersion < 1 {
                // Add subTitle 컬럼
            }
            
            if oldSchemaVersion < 2 {
                
                // 컬럼 이름 변경 -> renameProperty 붙이지 않고 컬럼명 변경 후 migration 시 기존 데이터 모두 null
                migration.renameProperty(onType: BookRealmModel.className(), from: "overview", to: "overView")
            }
            
            if oldSchemaVersion < 3 {
                
                // 기존 컬럼의 기본값 총괄 변경, 컬럼 타입 변경, 기존 컬럼 값 결합 등
                migration.enumerateObjects(ofType: BookRealmModel.className()) { oldObject, newObject in
                    guard let new = newObject else { return }
                    guard let old = oldObject else { return }
                    
                    new["comment"] = "\(old["title"]!) 이 영화 한번 봐보세요."
                }
            }
        }
        
        Realm.Configuration.defaultConfiguration = config
        
        return true
    }
  • Migration을 진행할 때는 항상 상위 버전으로 업데이트
  • 릴리즈 버전이 아닌 디버깅 버전에서는 → deleteRealmIfMigrationNeeded 사용
  • Migration의 블록은 중첩되거나 건너뛸 수 없다 → if (oldSchemaVersion < X)
  • schemaVersion이 바뀌면 샌드박스 구조가 변경되어 경로가 바뀜

[Change an Object Model - Swift SDK]

@kimkyuchul kimkyuchul added the IOS label Sep 6, 2023
@kimkyuchul kimkyuchul self-assigned this Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant