728x90
๋ฐ์ํ
๋ชจ๋ ๊ฐ์ฒด ์กฐํ
- ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ์กฐํํ๊ณ ์ถ์ ๊ฒฝ์ฐ 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/
let predicate = NSPredicate(format: "name == '์ ์ธ์'")
let test1 = realm.objects(Customer.self).filter(predicate)
- NSPredicate๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ํํฐ ์กฐ๊ฑด์ ์ง์ ํด ์ค ์ ์์
- cheetsheet์ NSPredicate์์ ์ฌ์ฉํ ์ ์๋ ํ์์ด ์ ๋ฆฌ๋์ด ์์
let test1 = realm.objects(Customer.self).filter("name == '์ ์ธ์'")
- ๋ฐ๋ก filter์ ์คํธ๋ง์ผ๋ก ๋ฃ๋ ๊ฒ๋ ๊ฐ๋ฅ (์คํธ๋ง ๋ง๊ณ ์์๊น.. ใ ใ ๊ธ์ ํ๋ฆฌ๋ฉด ์ฑ์ด ํฐ์ง ์๋..)
์ฟผ๋ฆฌ๋ก ์กฐํํ๊ธฐ
- Realm ๋ฐ์ดํฐ๋ฒ ์ด์ค์ idx๊ฐ 1 ~ 8 ๊น์ง ์ด 8๊ฐ์ Customer ๊ฐ์ฒด๊ฐ ์๊ณ ์ฑ๋ง ๋ค๋ฅธ ์ด๋ฆ๊ณผ ๋๋ฌธ์ ์๋ฌธ์๊ฐ ๋ค๋ฅธ kim, Kim ์ฑ์ ๊ฐ์ง ์ฌ๋์ด ์์
- Product ๊ฐ์ฒด๋ 1, 2, 8 ๋ง ๊ฐ์ง๊ณ ์์
let test1 = realm.objects(Customer.self).filter("name == '์ ์ธ์'")
Results<Customer> <0x7fddc640aee0> (
[0] Customer {
idx = 3;
name = ์ ์ธ์;
privateType = 0;
products = List<Product> <0x6000011848f0> (
);
}
)
- name์ด '์ ์ธ์'์ธ ๊ฐ์ฒด๋ง์ ๋ฐํ
let test2 = realm.objects(Customer.self).filter("name BEGINSWITH 'kim'")
Results<Customer> <0x7fddc6514fe0> (
[0] Customer {
idx = 6;
name = kim miyoung;
privateType = 0;
products = List<Product> <0x6000011849a0> (
);
}
)
- BEGINSWITH๋ ๊ทธ ๋ค์ ์ค๋ ๊ฐ์ผ๋ก ์์ํ๋์ง๋ฅผ ํํฐ๋งํด์ ๋ฆฌํดํจ
- ์ด๋ kim์ ๊ฐ์ง ๊ฐ์ฒด๋ง ๋ฐํ๋๊ณ Kim ๋๋ฌธ์๋ก ์์ํ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ์ง ์์ ๊ฒ์ ํ์ธํ ์ ์์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ๊ธฐ ๋๋ฌธ์, ๊ตฌ๋ถ ์์ด ์กฐํํ๊ณ ์ถ๋ค๋ฉด BEGINSWITH ๋ค์์ [c] ๋ฅผ ์ ๋ ฅํด์ฃผ์ด์ผ ํจ
let test3 = realm.objects(Customer.self).filter("name BEGINSWITH 'Kim'")
Results<Customer> <0x7fddc6516aa0> (
[0] Customer {
idx = 7;
name = Kim minyoung;
privateType = 0;
products = List<Product> <0x600001184bb0> (
);
}
)
- ๋ง์ฐฌ๊ฐ์ง๋ก ๋๋ฌธ์ K๋ฅผ ๊ฐ์ง Kim์ผ๋ก ์์ํ๋ ๊ฐ์ฒด๋ฅผ ์กฐํํ์ฌ ๋ฐํํจ
let test4 = realm.objects(Customer.self).filter("name CONTAINS '์ธ์'")
Results<Customer> <0x7fddc6517ba0> (
[0] Customer {
idx = 3;
name = ์ ์ธ์;
privateType = 0;
products = List<Product> <0x600001184dc0> (
);
},
[1] Customer {
idx = 5;
name = ๊น์ธ์;
privateType = 1;
products = List<Product> <0x600001184e70> (
);
}
)
- BEGINSWITH๊ฐ ์์ํ๋ ์กฐ๊ฑด์ด์๋ค๋ฉด CONTAINS๋ ํด๋น ๊ฐ์ ํฌํจํ ๊ฐ์ฒด๋ฅผ ์ฐพ์
- ๋ฐ๋ผ์ '์ธ์'์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ๋ชจ๋ ๋ฐํํ๋ ๊ฒ์ ํ์ธ ํ ์ ์์
let test5 = realm.objects(Customer.self).filter("name CONTAINS [c] 'kim'")
Results<Customer> <0x7fddc6518ee0> (
[0] Customer {
idx = 6;
name = kim miyoung;
privateType = 0;
products = List<Product> <0x600001184f20> (
);
},
[1] Customer {
idx = 7;
name = Kim minyoung;
privateType = 0;
products = List<Product> <0x600001184fd0> (
);
}
)
- ๋์๋ฌธ์ ๊ตฌ๋ถํ์ง ์๊ณ kim์ด๋ผ๋ ๋จ์ด๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํจ
let test6 = realm.objects(Customer.self).filter("ANY products.price == 490")
Results<Customer> <0x7fddc640be00> (
[0] Customer {
idx = 8;
name = ์ฅ์ธ์;
privateType = 0;
products = List<Product> <0x600001185340> (
[0] Product {
name = ๊ณผ์;
price = 490;
}
);
}
)
- LinkingObjects๋ก ์ฐ๊ฒฐ๋ ๊ฐ๋ ์กฐํ๊ฐ ๊ฐ๋ฅํจ.
- Customer ๊ฐ์ฒด์ Products๊ฐ์ฒด๋ฅผ ์ด๋ ์ด๋ก ๊ฐ์ง๊ณ ์๋๋ฐ ์ด๋ ์ด ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ Product์ price๊ฐ 490์ธ ๊ฐ์ฒด๋ฅผ ์ฐพ์์ ๋ฐํํจ.
- ๋ฐํํ์ ์ Customer ๊ฐ์ฒด
let test7 = realm.objects(Customer.self).filter("ANY products.name == '๊ณผ์'")
Results<Customer> <0x7fddc651b290> (
[0] Customer {
idx = 8;
name = ์ฅ์ธ์;
privateType = 0;
products = List<Product> <0x6000011853f0> (
[0] Product {
name = ๊ณผ์;
price = 490;
}
);
}
)
- ์์ ๋์ผํ๊ฒ ๋์ํจ
let test8 = realm.objects(Customer.self).filter("idx IN {3, 4}")
i.e. name IN {‘Milk’, ‘Eggs’, ‘Bread’}
Results<Customer> <0x7fddc640c9c0> (
[0] Customer {
idx = 3;
name = ์ ์ธ์;
privateType = 0;
products = List<Product> <0x600001188370> (
);
},
[1] Customer {
idx = 4;
name = ๋ฌธ์ธ์;
privateType = 0;
products = List<Product> <0x6000011886e0> (
);
}
)
- IN์ ์ด์ฉํ๋ฉด ํ๋ฒ์ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ ์กฐํํ๋ ๊ฒ๋ ๊ฐ๋ฅํจ
๋ด๋ฆผ์ฐจ์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
func sorted(byKeyPath keyPath: String, ascending: Bool = true) -> Results<Element> {
sorted(by: [SortDescriptor(keyPath: keyPath, ascending: ascending)])
}
- byKeyPath๋ก ์ ๋ ฌ์ ์ฌ์ฉ๋ ํ๋๋ฅผ ์ง์ ํด ์ค
- ์ค๋ฆ์ฐจ์์ด default๋ก true๋ก ๋์ด ์์. false๋ก ๊ฐ์ ์ ๋ฌํ๋ฉด ๋ด๋ฆผ์ฐจ์
let test4 = realm.objects(Customer.self).filter("name CONTAINS '์ธ์'").sorted(byKeyPath: "idx", ascending: false)
Results<Customer> <0x7f924dc0ce70> (
[0] Customer {
idx = 5;
name = ๊น์ธ์;
privateType = 1;
products = List<Product> <0x600000c08790> (
);
},
[1] Customer {
idx = 3;
name = ์ ์ธ์;
privateType = 0;
products = List<Product> <0x600000c08840> (
);
}
)
- idx๊ฐ ๋ด๋ฆผ์ฐจ์ ์์ผ๋ก ๊ฐ์ฒด๊ฐ ๋ฐํ๋ ๊ฒ์ ํ์ธํ ์ ์์
์ฐธ๊ณ ๋ฐ ์ถ์ฒ ์ฌ์ดํธ
https://ali-akhtar.medium.com/crud-operation-using-realmswift-part-1-17a99de83cc1
728x90
๋ฐ์ํ
'๐ iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[RxSwift] Input Output ViewModel with MVVMC (0) | 2022.10.09 |
---|---|
[RealmSwift] Migration, ์ด์ ์ค์ธ ์ฑ์ ํ๋ ์ถ๊ฐํ ํ ๋ง์ด๊ทธ๋ ์ด์ ํด์ฃผ๊ธฐ (0) | 2022.10.02 |
[RealmSwift] LinkingObjects์ Realm Studio (0) | 2022.10.02 |
[RealmSwift] primaryKey์ AutoIncrease (0) | 2022.10.02 |
[RealmSwift] ๋ฐ์ดํฐ ๋ชจ๋ธ์ enum์ผ๋ก ํ์ ์ง์ ํ๊ธฐ (0) | 2022.10.02 |
๋๊ธ