๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[RealmSwift] Migration, ์šด์˜ ์ค‘์ธ ์•ฑ์˜ ํ•„๋“œ ์ถ”๊ฐ€ํ•œ ํ›„ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•ด์ฃผ๊ธฐ

by ํ‹ด๋”” 2022. 10. 2.
728x90
๋ฐ˜์‘ํ˜•
class Customer: Object {
    
    @objc dynamic var idx = 0
    @objc private dynamic var privateType: Int = CustomerType.active.rawValue
    let products = List<Product>()
    
}
  • ์ด๋ฏธ ์šด์˜ ์ค‘์ธ ์•ฑ์—(ํ˜น์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ์ค‘) 3๊ฐœ์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ์Œ. ์ด๋ฏธ realm์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด realm ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— add ํ•ด์ค€ ํ›„ ๋‚˜์ค‘์— ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์„ฑํ•˜๋ ค ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ

  • Realm Studio๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐํšŒํ•ด ๋ณด๋ฉด ์ด๋ฏธ 3๊ฐœ์˜ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ๊ฐ’๋งŒ ์ง€์ •๋˜์–ด ์žˆ์Œ
class Customer: Object {
    
    @objc dynamic var idx = 0
    @objc dynamic var name = "๋ฌด๋ช…"
    @objc private dynamic var privateType: Int = CustomerType.active.rawValue
    let products = List<Product>()
}
  • ์—ฌ๊ธฐ์— name์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ์กด์— ์ƒ์„ฑ๋œ Customer ๊ฐ์ฒด๋Š” name์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ
  • error ๋ฅผ ํ™•์ธํ•˜๋ฉด "Migration is required due to the following errors: Property 'customer.name' has been added 
class AppDelegate: UIResponder, UIApplicationDelegate {
    func applicationDidFinishLaunching(_ application: UIApplication) {
        
    }
}
  • ์•ฑ์ด ์‹คํ–‰ ๋œ ํ›„ ์ดˆ๊ธฐ ์ž‘์—…์„ ์œ„ํ•ด AppDelegate์—์„œ applicationDidFinishLauncing์—์„œ ์ž‘์„ฑ
    func applicationDidFinishLaunching(_ application: UIApplication) {
        let configuration = Realm.Configuration(schemaVersion: 2) { migration, oldSchemaVersion in
            migration.enumerateObjects(ofType: Customer.className()) { oldObject, newObject in
                print(oldObject ?? "์—†์Œ")
                newObject!["name"] = "๋ฌด๋ช…"
            }
        }
        
        Realm.Configuration.defaultConfiguration = configuration
    }

ํ•œ ์ค„์”ฉ ์‚ดํŽด ๋ณด๋ฉด,

Realm.Configuration(schemaVersion: 2)
  • ํ˜„์žฌ ์Šคํ‚ค๋งˆ ๋ฒ„์ „์„ ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Œ
        let configuration = Realm.Configuration(schemaVersion: 2) { migration, oldSchemaVersion in

        }
  • migrationBlock์—๋Š” migration๊ณผ ์ด์ „ ์Šคํ‚ค๋งˆ ๋ฒ„์ „์„ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
            migration.enumerateObjects(ofType: Customer.className()) { oldObject, newObject in
                print(oldObject ?? "์—†์Œ")
                newObject!["name"] = "๋ฌด๋ช…"
            }
  • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ์œ„ํ•œ ๊ฐ์ฒด์˜ ํด๋ ˆ์Šค ์ด๋ฆ„์„ ์ „๋‹ฌํ•˜๋ฉด ๋ธ”๋ก์—์„œ ์ด์ „ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ „ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Œ(๊ธฐ์กด์— ์žˆ๋˜ ๊ฐ’์„ ํ†ตํ•ด ์กฐํ•ฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํ•„๋“œ์˜ ๊ฐ’์„ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
  • newObject์—์„œ ํ•„๋“œ์ด๋ฆ„์„ ํ‚ค๊ฐ’์œผ๋กœ ํ•˜์—ฌ, ์ด๋ฒˆ ๋ฒ„์ „์— ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ํ•„๋“œ์˜ ๊ฐ’์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Œ
Realm.Configuration.defaultConfiguration = configuration
  • ๊ผญ ๋ณ€๊ฒฝ๋œ configuration๊ฐ์ฒด๋ฅผ Realm.Configuration์˜ defaultConfiguration์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•จ

  • RealmStudio์—์„œ nameํ•„๋“œ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Œ

 

์ถœ์ฒ˜ : Realm ํ™ˆํŽ˜์ด์ง€

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€