๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

realmswift4

[RealmSwift] Migration, ์šด์˜ ์ค‘์ธ ์•ฑ์˜ ํ•„๋“œ ์ถ”๊ฐ€ํ•œ ํ›„ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•ด์ฃผ๊ธฐ class Customer: Object { @objc dynamic var idx = 0 @objc private dynamic var privateType: Int = CustomerType.active.rawValue let products = List() } ์ด๋ฏธ ์šด์˜ ์ค‘์ธ ์•ฑ์—(ํ˜น์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ์ค‘) 3๊ฐœ์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ์Œ. ์ด๋ฏธ realm์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด realm ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— add ํ•ด์ค€ ํ›„ ๋‚˜์ค‘์— ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์„ฑํ•˜๋ ค ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ Realm Studio๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐํšŒํ•ด ๋ณด๋ฉด ์ด๋ฏธ 3๊ฐœ์˜ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ๊ฐ’๋งŒ ์ง€์ •๋˜์–ด ์žˆ์Œ class Customer: Object { @objc dynamic var idx = 0 @objc dynamic var name = "๋ฌด๋ช…" @o.. 2022. 10. 2.
[RealmSwift] ๊ฐ์ฒด ์กฐํšŒํ•˜๊ธฐ ๋ชจ๋“  ๊ฐ์ฒด ์กฐํšŒ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ objects ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ let allProduct = realm.objects(Product.self) PrimaryKey๋ฅผ ์ด์šฉํ•œ ์กฐํšŒ primary key์˜ ๊ฒฝ์šฐ int์™€ String ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ณ ์œ ์˜ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” String์„ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•จ let customer = realm.object(ofType: Customer.self, forPrimaryKey: 1) ์ด๋•Œ primaryKey ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ์—†์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ๋ฆฌํ„ด๋˜๋Š” ๊ฐ์ฒด๋Š” ์˜ต์…”๋„ NSPredicate ์ฟผ๋ฆฌ ์ƒ์„ฑ cheetsheet : https://academy.realm.io/posts/nspredicate-cheatsheet/ le.. 2022. 10. 2.
[RealmSwift] LinkingObjects์™€ Realm Studio ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ…Œ์ด๋ธ”๊ณผ ํ…Œ์ด๋ธ”๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ์Œ LinkingObjects๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 1:n, 1:1 ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ์Œ class Customer: Object { @objc dynamic var idx = 0 let products = List() } Object๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค์—์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์„ฑํ•ด ์คŒ array ํƒ€์ž…์œผ๋กœ์„œ Customer์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ Product๋ฅผ ๊ฐ€์ง Customer ๊ฐ์ฒด์—๋Š” Product์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ์ƒ๊น€ class Product: Object { @objc dynamic var name = "" @objc dynamic var price = 0 let ofCustomer = LinkingObjects(fromType: Customer.se.. 2022. 10. 2.
[RealmSwift] ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์— enum์œผ๋กœ ํƒ€์ž… ์ง€์ •ํ•˜๊ธฐ ํ•„๋“œ ๊ฐ’์˜ ์ง€์ •๋œ ํƒ€์ž…์ด ์žˆ๊ฑฐ๋‚˜ ๋“ฑ๊ธ‰์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋“ฑ์— enum์˜ ์žฅ์ ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Œ enum์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ •ํ•ด์ง„ ์ž…๋ ฅ๊ฐ’ ๋‚ด์—์„œ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š์Œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ String์ด๋‚˜ Int๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ •ํ•ด์ง„ ํƒ€์ž…์„ ์ž…๋ ฅํ•˜๋ฏ€๋กœ ์ข€๋” ์ง๊ด€์ ์ž„ enum CustomerType: Int { /// ํ™œ๋™ ๊ณ„์ •, default case active /// ํœด๋ฉด ๊ณ„์ • case dormant /// ์ •์ง€ ๊ณ„์ • case suspension } CustomerType enum์„ ์ •์˜ํ•ด ์คŒ (enum์—์„œ ๊ฐ case์˜ ๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ •์˜ํ•ด์ค€ ์ˆœ์„œ๋Œ€๋กœ 0, 1, 2.. ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์žฅ ์ฒ˜์Œ ์ •์˜๋œ case์— 71์„ ์ง€์ •ํ•ด ์ฃผ์—ˆ๋‹ค๋ฉด 71, 72, 73... ์ˆœ์œผ๋กœ ๊ฐ’์„.. 2022. 10. 2.