Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ - SwiftUI

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ๋น ๋ฅธ ์“ฐ๊ธฐ ์ˆ˜ํ–‰
  • ๊ฐ์ฒด์˜ ์†์„ฑ ์—…๋ฐ์ดํŠธ
  • ๊ด€์ฐฐ ๊ฒฐ๊ณผ ์ปฌ๋ ‰์…˜์—์„œ ๊ฐ์ฒด ์ถ”๊ฐ€ ๋˜๋Š” ์ œ๊ฑฐํ•˜๊ธฐ
  • ๋ชฉ๋ก์— ๊ฐ์ฒด ์ถ”๊ฐ€ํ•˜๊ธฐ
  • ์ƒ์„ฑํ•˜๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Realm ๊ฐ์ฒด๋ฅผ Realm์— ๋ณต์‚ฌํ•˜๊ธฐ
  • ๋ช…์‹œ์  ์“ฐ๊ธฐ ์ˆ˜ํ–‰

ํŠธ๋žœ์žญ์…˜ ๋ธ”๋ก ๋‚ด์—์„œ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„, Realm Swift SDK๋Š” ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์„ ๋ช…์‹œ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ ๋„ ๋น ๋ฅธ ์“ฐ๊ธฐ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

@ObservedRealmObject ๋˜๋Š” @ObservedResults ์†์„ฑ ๋ž˜ํผ(wrapper)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์„ ์•”์‹œ์ ์œผ๋กœ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด $ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Realm ๊ฐ์ฒด๋‚˜ ์ปฌ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์•”์‹œ์  ์“ฐ๊ธฐ๊ฐ€ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.

Realm SwiftUI ์†์„ฑ ๋ž˜ํผ(wrapper)๋Š” ๊ณ ์ •๋œ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์ž‘๋™ํ•˜์—ฌ ์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. $์„(๋ฅผ) ์‚ฌ์šฉํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์„ ๋งŒ๋“ค๋ฉด Realm Swift SDK๊ฐ€ ๋™๊ฒฐ๋œ ๊ฐ์ฒด๋ฅผ ๋™๊ฒฐ ํ•ด์ œํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด์— ์“ธ ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์‹œ์—์„œ๋Š” ์ƒํƒœ ๊ฐ์ฒด์˜ ์†์„ฑ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. $dog.favoriteToy์€(๋Š”) ๋ชจ๋ธ ๊ฐœ ๊ฐ์ฒด์˜ favoriteToy ์†์„ฑ์— ๋Œ€ํ•œ ๋ฐ”์ธ๋”ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ ์•ฑ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉด Realm์€ ์•”์‹œ์  ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์„ ์—ด๊ณ  ์ƒˆ ๊ฐ’์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

struct EditDogDetails: View {
@ObservedRealmObject var dog: Dog
var body: some View {
VStack {
Text(dog.name)
.font(.title2)
TextField("Favorite toy", text: $dog.favoriteToy)
}
}
}

์ผ๋ฐ˜ Realm ๊ฒฐ๊ณผ ์ปฌ๋ ‰์…˜ ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ ObservedResults ๋Š” ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ปฌ๋ ‰์…˜ ์ž…๋‹ˆ๋‹ค. ๋ฐ”์ธ๋”ฉ๋œ ์ปฌ๋ ‰์…˜ ์„ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉด Realm ์€ ์•”์‹œ์  ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜ (write transaction) (write transaction)์„ ์—ด๊ณ  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ปฌ๋ ‰์…˜ ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์‹œ์—์„œ๋Š” onDelete์—์„œ $dogs.remove์„(๋ฅผ) ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ ์„ธํŠธ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ $dogs์„(๋ฅผ) ์‚ฌ์šฉํ•˜๋ฉด BoundCollection์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์ด ์ƒ์„ฑ๋˜์–ด @ObservedResults dogs ์ปฌ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

addDogButton์— $dogs.append์„(๋ฅผ) ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์กฐ์น˜๋Š” @ObservedResults ์ปฌ๋ ‰์…˜์— ์ง์ ‘ ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

struct DogsListView: View {
@ObservedResults(Dog.self) var dogs
var body: some View {
NavigationView {
VStack {
// The list shows the dogs in the realm.
List {
ForEach(dogs) { dog in
DogRow(dog: dog)
// Because `$dogs` here accesses an ObservedResults
// collection, we can remove the specific dog from the collection.
// Regular Realm Results are immutable, but you can write directly
// to an `@ObservedResults` collection.
}.onDelete(perform: $dogs.remove)
}.listStyle(GroupedListStyle())
.navigationBarTitle("Dogs", displayMode: .large)
.navigationBarBackButtonHidden(true)
// Action bar at bottom contains Add button.
HStack {
Spacer()
Button(action: {
// The bound collection automatically
// handles write transactions, so we can
// append directly to it. This example assumes
// we have some values to populate the Dog object.
$dogs.append(Dog(value: ["name":"Bandido"]))
}) { Image(systemName: "plus") }
.accessibilityIdentifier("addDogButton")
}.padding()
}
}
}
}

์ฐธ๊ณ 

@ObservedResults ์†์„ฑ ๋ž˜ํผ(wrapper)๋Š” SwiftUI ๋ณด๊ธฐ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. View ๋ชจ๋ธ์—์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ด€์ฐฐํ•˜๋ ค๋ฉด ๋ณ€๊ฒฝ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•˜์‹ญ์‹œ์˜ค.

๋ชฉ๋ก ์†์„ฑ์ด ์žˆ๋Š” @ObservedRealmObject์™€(๊ณผ) ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ชฉ๋ก์— ์ƒˆ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์‹œ์—์„œ Person ๊ฐ์ฒด์—๋Š” ํ•œ ๋งˆ๋ฆฌ ์ด์ƒ์˜ ๊ฐœ์™€ ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ํ˜•์„ฑํ•˜๋Š” ๋ชฉ๋ก ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

class Person: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var firstName = ""
@Persisted var lastName = ""
...
@Persisted var dogs: List<Dog>
}

์‚ฌ์šฉ์ž๊ฐ€ Save ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์„ธ๋ถ€ ์ •๋ณด๋กœ Dog ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

  • Dog ๊ฐ์ฒด๋ฅผ Person ๊ฐ์ฒด์˜ dogs ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

struct AddDogToPersonView: View {
@ObservedRealmObject var person: Person
@Binding var isInAddDogView: Bool
@State var name = ""
@State var breed = ""
@State var weight = 0
@State var favoriteToy = ""
@State var profileImageUrl: URL?
var body: some View {
Form {
TextField("Dog's name", text: $name)
TextField("Dog's breed", text: $breed)
TextField("Dog's weight", value: $weight, format: .number)
TextField("Dog's favorite toy", text: $favoriteToy)
TextField("Image link", value: $profileImageUrl, format: .url)
.keyboardType(.URL)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
Section {
Button(action: {
let dog = createDog(name: name, breed: breed, weight: weight, favoriteToy: favoriteToy, profileImageUrl: profileImageUrl)
$person.dogs.append(dog)
isInAddDogView.toggle()
}) {
Text("Save")
}
Button(action: {
isInAddDogView.toggle()
}) {
Text("Cancel")
}
}
}
}
}

์ƒˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์†์„ฑ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜์—ญ์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ƒˆ ๊ฐ์ฒด๋ฅผ ์˜์—ญ์— ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ์œ ์‚ฌํ•œ ์˜ค๋ฅ˜๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

Object is already managed by another Realm. Use create instead to copy it into this Realm.

์ด ๊ฒฝ์šฐ .create ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  modified: .update ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์†์„ฑ์„ ๊ธฐ์กด ๊ฐ์ฒด๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ

favoriteToy ์†์„ฑ์ด ๋‹จ์ˆœํ•œ String์ด(๊ฐ€) ์•„๋‹ˆ๋ผ ์„ ํƒ์  DogToy ๊ฐ์ฒด์ธ DoggoDB Dog ๋ชจ๋ธ ๋ฒ„์ „์„ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”.

class Dog: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: UUID
@Persisted var name = ""
...
@Persisted var favoriteToy: DogToy?
...
}

์•ฑ์ด ์ƒˆ Dog ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๊ณ  ํ•  ๋•Œ DogToy ๊ฐ์ฒด๊ฐ€ ์ด๋ฏธ ์˜์—ญ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ ๋‹ค์Œ favoriteToy ์†์„ฑ์„ ๊ธฐ์กด ๊ฐ•์•„์ง€ ์žฅ๋‚œ๊ฐ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒˆ Dog Person ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ์œ ์‚ฌํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Object is already managed by another Realm. Use create instead to copy it into this Realm.

Dog ๊ฐ์ฒด๋Š” Person ๊ฐ์ฒด์˜ dogs ์†์„ฑ์— ์ถ”๊ฐ€๋  ๋•Œ๊นŒ์ง€ ๊ด€๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ƒํƒœ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. Realm Swift SDK๊ฐ€ Dog ๊ฐ์ฒด๋ฅผ ํ™•์ธํ•˜์—ฌ ํ˜„์žฌ ๊ด€๋ฆฌ ์ค‘์ธ ์˜์—ญ์„ ์ฐพ์•„๋„ ์•„๋ฌด๊ฒƒ๋„ ๋ฐœ๊ฒฌํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

$ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ Dog ๊ฐ์ฒด๋ฅผ Person ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜๋Š” ๋น ๋ฅธ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ด ์“ฐ๊ธฐ๋Š” ๋ณด๊ธฐ์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” @ObservedRealmObject ๋˜๋Š” @ObservedResults ์†์„ฑ ๋ž˜ํผ(wrapper)์— ์˜ํ•ด ์•”์‹œ์ ์œผ๋กœ ์—ด๋ฆฌ๋Š” ์˜์—ญ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ์กด DogToy ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ์˜์—ญ ์ธ์Šคํ„ด์Šค์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด .create ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Dog ๊ฐ์ฒด ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  modified: .update ๋ฅผ ์„ค์ •ํ•˜๋‹ค ํ•˜์—ฌ ํ•ด๋‹น favoriteToy ๊ฐ’์„ ๊ธฐ์กด ๊ฐ์ฒด ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

// When working with an `@ObservedRealmObject` `Person`, this is a frozen object.
// Thaw the object and get its realm to perform the write to append the new dog.
let thawedPersonRealm = frozenPerson.thaw()!.realm!
try! thawedPersonRealm.write {
// Use the .create method with `update: .modified` to copy the
// existing object into the realm
let dog = thawedPersonRealm.create(Dog.self, value:
["name": "Maui",
"favoriteToy": wubba],
update: .modified)
person.dogs.append(dog)
}

๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋น ๋ฅธ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์•”์‹œ์ ์ธ $์„(๋ฅผ) ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ๋ช…์‹œ์ ์œผ๋กœ ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์— ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์ถ”๊ฐ€ ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ณด๊ธฐ์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์—†๋Š” ๊ฐ์ฒด์— ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@ObservedRealmObject ๋˜๋Š” @ObservedResults(์œผ)๋กœ ๊ด€์ฐฐ ์ค‘์ธ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ช…์‹œ์  ์“ฐ๊ธฐ ํŠธ๋žœ์žญ์…˜(write transaction)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ ๋จผ์ € ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๊ณ ์ • ํ•ด์ œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

let thawedCompany = company.thaw()!

๊ฐ์ฒด ๋˜๋Š” ์ปฌ๋ ‰์…˜์—์„œ .realm์„(๋ฅผ) ํ˜ธ์ถœํ•˜์—ฌ ์˜์—ญ ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์˜์—ญ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let realm = company.realm!.thaw()

SwiftUI ์†์„ฑ ๋ž˜ํผ(wrapper)๋Š” ๊ณ ์ •๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜์—ญ ๊ฐ์ฒด์— ์“ฐ๊ธฐ ์ „์— ์˜์—ญ์„ ์žฌ๊ฐœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ

Company ๊ฐ์ฒด์— Employee ๊ฐ์ฒด ๋ชฉ๋ก์ด ์žˆ๋Š” DoggoDB ์•ฑ ๋ฒ„์ „์„ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. ๊ฐ Employee์—๋Š” Dog ๊ฐ์ฒด ๋ชฉ๋ก์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋น„์ฆˆ๋‹ˆ์Šค์ƒ์˜ ์ด์œ ๋กœ Employee์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š๊ณ  Company ๊ฐ์ฒด์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Dog ๊ฐ์ฒด ๋ชฉ๋ก์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

class Company: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var companyName = ""
@Persisted var employees: List<Employee>
@Persisted var dogs: List<Dog>
}

Company ๊ฐ์ฒด์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ธฐ์กด ๊ฐœ๋ฅผ ๊ธฐ์กด ์ง์›์— ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๋ช…์‹œ์  ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๋Š” ๋ณด๊ธฐ๋ฅผ ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// The `frozenCompany` here represents an `@ObservedRealmObject var company: Company`
performAnExplicitWrite(company: frozenCompany, employeeName: "Dachary", dogName: "Maui")
func performAnExplicitWrite(company: Company, employeeName: String, dogName: String) {
// Get the realm that is managing the `Company` object you passed in.
// Thaw the realm so you can write to it.
let realm = company.realm!.thaw()
// Thawing the `Company` object that you passed in also thaws the objects in its List properties.
// This lets you append the `Dog` to the `Employee` without individually thawing both of them.
let thawedCompany = company.thaw()!
let thisEmployee = thawedCompany.employees.where { $0.name == employeeName }.first!
let thisDog = thawedCompany.dogs.where { $0.name == dogName }.first!
try! realm.write {
thisEmployee.dogs.append(thisDog)
}
}

๋Œ์•„๊ฐ€๊ธฐ

๋ทฐ ๊ฐ„์— Realm ๋ฐ์ดํ„ฐ ์ „๋‹ฌ