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

[RealmSwift] ๊ฐ์ฒด ์กฐํšŒํ•˜๊ธฐ

by ํ‹ด๋”” 2022. 10. 2.
728x90
๋ฐ˜์‘ํ˜•

 

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

๋ชจ๋“  ๊ฐ์ฒด ์กฐํšŒ

  • ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ 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
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€