CRUD - ๋ง๋ค๊ธฐ - Swift SDK
์ด ํ์ด์ง์ ๋ด์ฉ
- ์ ๊ฐ์ฒด ๋ง๋ค๊ธฐ
- ์ด ํ์ด์ง์ ์์ ๊ด๋ จ ์ ๋ณด
- ๊ฐ์ฒด ์์ฑ
- ๊ฐ์ผ๋ก ๊ฐ์ฒด ์ด๊ธฐํ
- JSON์ผ๋ก ๊ฐ์ฒด ์์ฑ
- ๋ด์ฅ๋ ๊ฐ์ฒด ์์ฑ
- ์ง๋ ์์ฑ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด ์์ฑ
- MutableSet ์์ฑ์ผ๋ก ๊ฐ์ฒด ์์ฑ
- AnyRealmValue ์์ฑ์ผ๋ก ๊ฐ์ฒด ์์ฑ
- ๋น๋๊ธฐ์ ๊ฐ์ฒด ์์ฑ
- ๋น๋์นญ ๊ฐ์ฒด ์์ฑ
- ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ Realm์ ๋ณต์ฌ
์ ๊ฐ์ฒด ๋ง๋ค๊ธฐ
์ด ํ์ด์ง์ ์์ ๊ด๋ จ ์ ๋ณด
์ด ํ์ด์ง์ ์์์์๋ ๋ค์ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
// DogToy.h @interface DogToy : RLMObject @property NSString *name; @end // Dog.h @interface Dog : RLMObject @property NSString *name; @property int age; @property NSString *color; // To-one relationship @property DogToy *favoriteToy; @end // Enable Dog for use in RLMArray RLM_COLLECTION_TYPE(Dog) // Person.h // A person has a primary key ID, a collection of dogs, and can be a member of multiple clubs. @interface Person : RLMObject @property int _id; @property NSString *name; // To-many relationship - a person can have many dogs @property RLMArray<Dog *><Dog> *dogs; // Inverse relationship - a person can be a member of many clubs @property (readonly) RLMLinkingObjects *clubs; @end RLM_COLLECTION_TYPE(Person) // DogClub.h @interface DogClub : RLMObject @property NSString *name; @property RLMArray<Person *><Person> *members; @end // Dog.m @implementation Dog @end // DogToy.m @implementation DogToy @end // Person.m @implementation Person // Define the primary key for the class + (NSString *)primaryKey { return @"_id"; } // Define the inverse relationship to dog clubs + (NSDictionary *)linkingObjectsProperties { return @{ @"clubs": [RLMPropertyDescriptor descriptorWithClass:DogClub.class propertyName:@"members"], }; } @end // DogClub.m @implementation DogClub @end
class DogToy: Object { var name = "" } class Dog: Object { var name = "" var age = 0 var color = "" var currentCity = "" var citiesVisited: MutableSet<String> var companion: AnyRealmValue // To-one relationship var favoriteToy: DogToy? // Map of city name -> favorite park in that city var favoriteParksByCity: Map<String, String> } class Person: Object { true) var id = 0 (primaryKey: var name = "" // To-many relationship - a person can have many dogs var dogs: List<Dog> // Embed a single object. // Embedded object properties must be marked optional. var address: Address? convenience init(name: String, address: Address) { self.init() self.name = name self.address = address } } class Address: EmbeddedObject { var street: String? var city: String? var country: String? var postalCode: String? }
๊ฐ์ฒด ์์ฑ
Realm์ ๊ฐ์ฒด ๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ๋ค๋ฅธ ๊ฐ์ฒด ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํํ ๋ค์ ์ฐ๊ธฐ ํธ๋์ญ์ ( ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ) ๋ด์์ -[ ์์ญaddObject:] ์ ์ ๋ฌํฉ๋๋ค.
// Get the default realm. // You only need to do this once per thread. RLMRealm *realm = [RLMRealm defaultRealm]; // Instantiate the class. Dog *dog = [[Dog alloc] init]; dog.name = @"Max"; dog.age = 5; // Open a thread-safe transaction. [realm transactionWithBlock:^() { // Add the instance to the realm. [realm addObject:dog]; }];
์์ญ์ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ๋ค๋ฅธ ๊ฐ์ฒด์ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํํ ๋ค์ ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ Realm.add(_:update:)๋ก ์ ๋ฌํฉ๋๋ค.
// Instantiate the class and set its values. let dog = Dog() dog.name = "Rex" dog.age = 10 // Get the default realm. You only need to do this once per thread. let realm = try! Realm() // Open a thread-safe transaction. try! realm.write { // Add the instance to the realm. realm.add(dog) }
๊ฐ์ผ๋ก ๊ฐ์ฒด ์ด๊ธฐํ
Object.init(value:) ์ ์ด๋์ ๋ผ์ด์ ๊ฐ์ ์ ๋ฌํ์ฌ ๊ฐ์ฒด ๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค. ์ด๋์ ๋ผ์ด์ ๊ฐ์ ํค-๊ฐ ์ฝ๋ฉ ์ ์์ต๋๋ค. ํธํ ๊ฐ์ฒด , ์ฌ์ ๋๋ ๊ฐ managed ์์ฑ ์ ๋ํด ํ๋์ ์์๋ฅผ ํฌํจํ๋ ๋ฐฐ์ด ์ ๋๋ค.
์ฐธ๊ณ
๋ฐฐ์ด์ ์ด๋์ ๋ผ์ด์ ๊ฐ์ผ๋ก ์ฌ์ฉํ ๋๋ ๋ชจ๋ธ์ ์ ์๋ ์์์ ๋์ผํ ์์๋ก ๋ชจ๋ ์์ฑ์ ํฌํจํด์ผ ํฉ๋๋ค.
// (1) Create a Dog object from a dictionary Dog *myDog = [[Dog alloc] initWithValue:@{@"name" : @"Pluto", @"age" : @3}]; // (2) Create a Dog object from an array Dog *myOtherDog = [[Dog alloc] initWithValue:@[@"Pluto", @3]]; RLMRealm *realm = [RLMRealm defaultRealm]; // Add to the realm with transaction [realm transactionWithBlock:^() { [realm addObject:myDog]; [realm addObject:myOtherDog]; }];
// (1) Create a Dog object from a dictionary let myDog = Dog(value: ["name": "Pluto", "age": 3]) // (2) Create a Dog object from an array let myOtherDog = Dog(value: ["Fido", 5]) let realm = try! Realm() // Add to the realm inside a transaction try! realm.write { realm.add([myDog, myOtherDog]) }
์ด๋์ ๋ผ์ด์ ๊ฐ์ ์ค์ฒฉํ์ฌ ๊ด๋ จ ๋๋ ๋ด์ฅ๋ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์๋ ์์ต๋๋ค.
// Instead of using pre-existing dogs... Person *aPerson = [[Person alloc] initWithValue:@[@123, @"Jane", @[aDog, anotherDog]]]; // ...we can create them inline Person *anotherPerson = [[Person alloc] initWithValue:@[@123, @"Jane", @[@[@"Buster", @5], @[@"Buddy", @6]]]];
// Instead of using pre-existing dogs... let aPerson = Person(value: [123, "Jane", [aDog, anotherDog]]) // ...we can create them inline let anotherPerson = Person(value: [123, "Jane", [["Buster", 5], ["Buddy", 6]]])
์ผ๋ถ ์์ฑ ์ ํ์ ์ฐ๊ธฐ ํธ๋์ญ์ ์์๋ง ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
์ผ๋ถ ์์ฑ ์ ํ์ ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction)์์๋ง ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด MutableSet ์์ฑ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํํ ์ ์์ง๋ง ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction)์์๋ ํด๋น ์์ฑ์ ๊ฐ๋ง ์ค์ ํ ์ ์์ต๋๋ค. ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ ์ด๊ธฐํํ์ง ์๋ ํ ํด๋น ์์ฑ์ ๊ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค.
JSON์ผ๋ก ๊ฐ์ฒด ์์ฑ
Realm ์ JSON ์ ์ง์ ์ง์ ํ์ง ์์ง๋ง JSONSerialization.jsonObject(with:options:) JSON ์ Realm.create(_:value: ์ ๋ฐ์ดํธ:)์ ์ ๋ฌํ ์ ์๋ ๊ฐ์ผ๋ก ๋ณํํฉ๋๋ค.
// Specify a dog toy in JSON NSData *data = [@"{\"name\": \"Tennis ball\"}" dataUsingEncoding: NSUTF8StringEncoding]; RLMRealm *realm = [RLMRealm defaultRealm]; // Insert from NSData containing JSON [realm transactionWithBlock:^{ id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; [DogToy createInRealm:realm withValue:json]; }];
// Specify a dog toy in JSON let data = "{\"name\": \"Tennis ball\"}".data(using: .utf8)! let realm = try! Realm() // Insert from data containing JSON try! realm.write { let json = try! JSONSerialization.jsonObject(with: data, options: []) realm.create(DogToy.self, value: json) }
JSON์ ์ค์ฒฉ๋ ๊ฐ์ฒด ๋๋ ๋ฐฐ์ด์ ์ผ๋์ผ ๋๋ ๋ค๋๋ค ๊ด๊ณ์ ๋งคํํฉ๋๋ค.
JSON ์์ฑ ์ด๋ฆ๊ณผ ์ ํ์ ๋์ ๊ฐ์ฒด ์คํค๋ง์ ์ ํํ ์ผ์นํด์ผ ํฉ๋๋ค. ์์:
float
์์ฑ์ float ์ง์NSNumbers
(์ผ)๋ก ์ด๊ธฐํ๋์ด์ผ ํฉ๋๋ค.Date
๋ฐData
์์ฑ์ ๋ฌธ์์ด์์ ์ ์ถํ ์ ์์ต๋๋ค. Realm.create(_:value:update:)์ ์ ๋ฌํ๊ธฐ ์ ์ ์ ์ ํ ์ ํ์ผ๋ก ๋ณํํฉ๋๋ค.ํ์ ์์ฑ์
null
์ด๊ฑฐ๋ JSON์ ๋๋ฝ๋ ์ ์์ต๋๋ค.
Realm์ ๊ฐ์ฒด ์คํค๋ง์ ์ ์๋์ง ์์ JSON์ ๋ชจ๋ ์์ฑ์ ๋ฌด์ํฉ๋๋ค.
ํ
JSON schema ๊ฐ Realm ๊ฐ์ฒด์ ์ ํํ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ ํ์ฌ ํ๋ ์์ํฌ ๋ฅผ ์ฌ์ฉํ์ฌ JSON ์ ๋ณํํ๋ ๊ฒ์ ๊ณ ๋ คํ์ธ์. Realm ๊ณผ ํจ๊ป ์๋ํ๋ ๋ชจ๋ธ ๋งคํ ํ๋ ์์ํฌ๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ต๋๋ค. Realm-swift ๋ฆฌํฌ์งํ ๋ฆฌ ์ ์ผ๋ถ ๋ชฉ๋ก์ ์ฐธ์กฐํ์ธ์.
๋ด์ฅ๋ ๊ฐ์ฒด ์์ฑ
๋ด์ฅ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด ๋ด์ฅ๋ ๊ฐ์ฒด์ ์ธ์คํด์ค๋ฅผ ์์ ๊ฐ์ฒด์ ์์ฑ์ ํ ๋นํฉ๋๋ค.
RLMRealm *realm = [RLMRealm defaultRealm]; [realm transactionWithBlock:^{ Address *address = [[Address alloc] init]; address.street = @"123 Fake St."; address.city = @"Springfield"; address.country = @"USA"; address.postalCode = @"90710"; Contact *contact = [Contact contactWithName:@"Nick Riviera"]; // Assign the embedded object property contact.address = address; [realm addObject:contact]; NSLog(@"Added contact: %@", contact); }];
// Open the default realm let realm = try! Realm() try! realm.write { let address = Address() address.street = "123 Fake St" address.city = "Springfield" address.country = "USA" address.postalCode = "90710" let contact = Person(name: "Nick Riviera", address: address) realm.add(contact) }
์ง๋ ์์ฑ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด ์์ฑ
์ง๋ ์์ฑ ์ด ์๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ ๋ค์๊ณผ ๊ฐ์ ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ํค ๊ฐ์ ์ค์ ํ ์ ์์ต๋๋ค.
๊ฐ์ฒด์ ํค์ ๊ฐ์ ์ค์ ํ ๋ค์ ๊ฐ์ฒด๋ฅผ ์์ญ์ ์ถ๊ฐ
์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ ์ง์ ๊ฐ์ฒด์ ํค์ ๊ฐ ์ค์
ํค-๊ฐ ์ฝ๋ฉ์ ์ฌ์ฉํ์ฌ ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ ํค์ ๊ฐ์ ์ค์ ๋๋ ์ ๋ฐ์ดํธ
let realm = try! Realm() // Record a dog's name and current city let dog = Dog() dog.name = "Wolfie" dog.currentCity = "New York" // Set map values dog.favoriteParksByCity["New York"] = "Domino Park" // Store the data in a realm try! realm.write { realm.add(dog) // You can also set map values inside a write transaction dog.favoriteParksByCity["Chicago"] = "Wiggly Field" dog.favoriteParksByCity.setValue("Bush Park", forKey: "Ottawa") }
Realm์์๋ ์ง๋ ํค์ .
๋๋ $
๋ฌธ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐฑ๋ถ์จ ์ธ์ฝ๋ฉ ๋ฐ ๋์ฝ๋ฉ์ ์ฌ์ฉํ์ฌ ํ์ฉ๋์ง ์๋ ๋ฌธ์ ์ค ํ๋๊ฐ ํฌํจ๋ ์ง๋ ํค๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค.
// Percent encode . or $ characters to use them in map keys let mapKey = "New York.Brooklyn" let encodedMapKey = "New York%2EBrooklyn"
MutableSet ์์ฑ์ผ๋ก ๊ฐ์ฒด ์์ฑ
๋ค๋ฅธ Realm ๊ฐ์ฒด์ ๋ง์ฐฌ๊ฐ์ง๋ก MutableSet ์์ฑ์ ํฌํจํ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์์ง๋ง ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์๋ง MutableSet๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์ด๋ ์ฐ๊ธฐ ํธ๋์ญ์ (write transaction) ๋ด์์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ค์ ์์ฑ์ ๊ฐ๋ง ์ค์ ํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
let realm = try! Realm() // Record a dog's name and current city let dog = Dog() dog.name = "Maui" dog.currentCity = "New York" // Store the data in a realm. Add the dog's current city // to the citiesVisited MutableSet try! realm.write { realm.add(dog) // You can only mutate the MutableSet in a write transaction. // This means you can't set values at initialization, but must do it during a write. dog.citiesVisited.insert(dog.currentCity) } // You can also add multiple items to the set. try! realm.write { dog.citiesVisited.insert(objectsIn: ["Boston", "Chicago"]) } print("\(dog.name) has visited: \(dog.citiesVisited)")
AnyRealmValue ์์ฑ์ผ๋ก ๊ฐ์ฒด ์์ฑ
AnyRealmValue ์์ฑ ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด ๋ฅผ ๋ง๋ค ๋๋ ์์ฑ ์ ์ ์ฅ ํ๋ ๊ฐ์ ์ ํ์ ์ง์ ํด์ผ ํฉ๋๋ค. Realm Swift SDK ๋ AnyRealmValue๊ฐ ์ ์ฅ ์ ์๋ ๋ชจ๋ ์ ํ์ ๋ฐ๋ณตํ๋ AnyRealmValue ์ด๊ฑฐํ ์ ์ ๊ณตํฉ๋๋ค.
์ถํ AnyRealmValue๋ฅผ ์ฝ์ ๋๋ ๊ฐ์ผ๋ก ์ด๋ค ์์ ์ ์ํํ๊ธฐ ์ ์ ์ ํ์ ํ์ธํด์ผ ํฉ๋๋ค.
// Create a Dog object and then set its properties let myDog = Dog() myDog.name = "Rex" // This dog has no companion. // You can set the field's type to "none", which represents `nil` myDog.companion = .none // Create another Dog whose companion is a cat. // We don't have a Cat object, so we'll use a string to describe the companion. let theirDog = Dog() theirDog.name = "Wolfie" theirDog.companion = .string("Fluffy the Cat") // Another dog might have a dog as a companion. // We do have an object that can represent that, so we can specify the // type is a Dog object, and even set the object's value. let anotherDog = Dog() anotherDog.name = "Fido" // Note: this sets Spot as a companion of Fido, but does not set // Fido as a companion of Spot. Spot has no companion in this instance. anotherDog.companion = .object(Dog(value: ["name": "Spot"])) // Add the dogs to the realm let realm = try! Realm() try! realm.write { realm.add([myDog, theirDog, anotherDog]) } // After adding these dogs to the realm, we now have 4 dog objects. let dogs = realm.objects(Dog.self) XCTAssertEqual(dogs.count, 4)
๋น๋๊ธฐ์ ๊ฐ์ฒด ์์ฑ
Swift ๋์์ฑ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ํ์์๊ฐ ๊ฒฉ๋ฆฌ๋ ์์ญ์ ๋น๋๊ธฐ์ ์ผ๋ก ๊ธฐ๋กํ ์ ์์ต๋๋ค.
ํ์์์ Realm ์ฌ์ฉ ํ์ด์ง์ ์ ์๋ RealmActor
์์ ์ ํจ์๋ ํ์์ ๊ฒฉ๋ฆฌ ์์ญ์ ๊ธฐ๋กํ ์ ์๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
func createTodo(name: String, owner: String, status: String) async throws { try await realm.asyncWrite { realm.create(Todo.self, value: [ "_id": ObjectId.generate(), "name": name, "owner": owner, "status": status ]) } }
๊ทธ๋ฆฌ๊ณ Swift์ ๋น๋๊ธฐ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ด ์ฐ๊ธฐ๋ฅผ ์ํํ ์๋ ์์ต๋๋ค.
func createObject() async throws { // Because this function is not isolated to this actor, // you must await operations completed on the actor try await actor.createTodo(name: "Take the ring to Mount Doom", owner: "Frodo", status: "In Progress") let taskCount = await actor.count print("The actor currently has \(taskCount) tasks") } let actor = try await RealmActor() try await createObject()
์ด ์์ ์ ํธ์ถ ์ค๋ ๋์์ I/O๋ฅผ ์ฐจ๋จํ๊ฑฐ๋ ์ํํ์ง ์์ต๋๋ค. Swift ๋์์ฑ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ Realm์ ์ฐ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด Actors์ ํจ๊ป Realm ์ฌ์ฉ - Swift SDK๋ฅผ ์ฐธ์กฐํ์ธ์.
๋น๋์นญ ๊ฐ์ฒด ์์ฑ
๋ฒ์ 10.29.0์ ์๋ก์ด ๊ธฐ๋ฅ
AsymmetricObject๋ create(_ type:, value:) ๋ฅผ ํตํด์๋ง ๋ง๋ค ์ ์์ต๋๋ค. AsymmetricObject๋ฅผ ์์ฑํ๋ฉด ๋ฐ์ดํฐ ์์ง ์ ํตํด Atlas App Services ์ฑ์ ์ฐ๊ฒฐ๋ Atlas ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋จ๋ฐฉํฅ์ผ๋ก ๋๊ธฐํ๋ฉ๋๋ค. AsymmetricObject์ ๋ก์ปฌ๋ก ์ก์ธ์คํ๊ฑฐ๋ Realm์ ์ถ๊ฐ ๋๋ ์ ๊ฑฐํ๊ฑฐ๋ ์ฟผ๋ฆฌํ ์ ์์ต๋๋ค.
func useRealm(_ asymmetricRealm: Realm, _ user: User) async { try! asymmetricRealm.write { asymmetricRealm.create(WeatherSensor.self, value: [ "_id": ObjectId.generate(), "deviceId": "WX1278UIT", "temperatureInFahrenheit": 66.7, "barometricPressureInHg": 29.65, "windSpeedInMph": 2 ]) } }
Flexible Sync ๊ตฌ์ฑ ์ผ๋ก ์ด๊ธฐํ๋ Realm์ ๋ํด ์์ญ ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ ์ Flexible Sync๋ฅผ ์ํด ๋๊ธฐํ๋ Realm ์ด๊ธฐ๋ฅผ ์ฐธ์กฐํ์ธ์.
๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ Realm์ ๋ณต์ฌ
Realm ๊ฐ์ ๊ฐ์ฒด ๋ฅผ ๋ณต์ฌํ๋ ค๋ฉด ์๋ณธ ๊ฐ์ฒด ๋ฅผ +[ ์์ญ createInRealm:withValue:]์ ์ ๋ฌํฉ๋๋ค.
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration]; configuration.inMemoryIdentifier = @"first realm"; RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:nil]; [realm transactionWithBlock:^{ Dog *dog = [[Dog alloc] init]; dog.name = @"Wolfie"; dog.age = 1; [realm addObject:dog]; }]; // Later, fetch the instance we want to copy Dog *wolfie = [[Dog objectsInRealm:realm where:@"name == 'Wolfie'"] firstObject]; // Open the other realm RLMRealmConfiguration *otherConfiguration = [RLMRealmConfiguration defaultConfiguration]; otherConfiguration.inMemoryIdentifier = @"second realm"; RLMRealm *otherRealm = [RLMRealm realmWithConfiguration:otherConfiguration error:nil]; [otherRealm transactionWithBlock:^{ // Copy to the other realm Dog *wolfieCopy = [[wolfie class] createInRealm:otherRealm withValue:wolfie]; wolfieCopy.age = 2; // Verify that the copy is separate from the original XCTAssertNotEqual(wolfie.age, wolfieCopy.age); }];
ํ Realm์์ ๋ค๋ฅธ Realm์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ๋ ค๋ฉด ์๋ณธ ๊ฐ์ฒด๋ฅผ Realm.create(_:value:update:)::
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "first realm")) try! realm.write { let dog = Dog() dog.name = "Wolfie" dog.age = 1 realm.add(dog) } // Later, fetch the instance we want to copy let wolfie = realm.objects(Dog.self).first(where: { $0.name == "Wolfie" })! // Open the other realm let otherRealm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "second realm")) try! otherRealm.write { // Copy to the other realm let wolfieCopy = otherRealm.create(type(of: wolfie), value: wolfie) wolfieCopy.age = 2 // Verify that the copy is separate from the original XCTAssertNotEqual(wolfie.age, wolfieCopy.age) }
์ค์
create
๋ฉ์๋๋ ์ฃผ๊ธฐ์ ๊ฐ์ฒด ๊ทธ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ง์ํ์ง ์์ต๋๋ค. ์์ ํญ๋ชฉ์ ์ง์ ๋๋ ๊ฐ์ ์ ์ผ๋ก ์ฐธ์กฐํ๋ ๊ฐ์ฒด์ ๊ด๋ จ๋ ๊ด๊ณ๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ง ์๋๋ก ํฉ๋๋ค.