๋ฐ์ํ
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ํ๋๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธ ํ ์ ์์
728x90
๋ฐ์ํ
'๐ iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[RxSwift] Traits์ Single๊ณผ Service for Network (0) | 2022.10.09 |
---|---|
[RxSwift] Input Output ViewModel with MVVMC (0) | 2022.10.09 |
[RealmSwift] ๊ฐ์ฒด ์กฐํํ๊ธฐ (0) | 2022.10.02 |
[RealmSwift] LinkingObjects์ Realm Studio (0) | 2022.10.02 |
[RealmSwift] primaryKey์ AutoIncrease (0) | 2022.10.02 |
๋๊ธ