Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

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 {
@Persisted var name = ""
}
class Dog: Object {
@Persisted var name = ""
@Persisted var age = 0
@Persisted var color = ""
@Persisted var currentCity = ""
@Persisted var citiesVisited: MutableSet<String>
@Persisted var companion: AnyRealmValue
// To-one relationship
@Persisted var favoriteToy: DogToy?
// Map of city name -> favorite park in that city
@Persisted var favoriteParksByCity: Map<String, String>
}
class Person: Object {
@Persisted(primaryKey: true) var id = 0
@Persisted var name = ""
// To-many relationship - a person can have many dogs
@Persisted var dogs: List<Dog>
// Embed a single object.
// Embedded object properties must be marked optional.
@Persisted var address: Address?
convenience init(name: String, address: Address) {
self.init()
self.name = name
self.address = address
}
}
class Address: EmbeddedObject {
@Persisted var street: String?
@Persisted var city: String?
@Persisted var country: String?
@Persisted 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) ๋‚ด์—์„œ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๋Š” ํ•œ ํ•ด๋‹น ์†์„ฑ์˜ ๊ฐ’์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

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"

๋‹ค๋ฅธ 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 ์†์„ฑ ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์†์„ฑ ์— ์ €์žฅ ํ•˜๋Š” ๊ฐ’์˜ ์œ ํ˜•์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 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์— ์ถ”๊ฐ€ ๋˜๋Š” ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

@MainActor
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 ๊ฐ„์— ๊ฐ์ฒด ๋ฅผ ๋ณต์‚ฌํ•˜๋ ค๋ฉด ์›๋ณธ ๊ฐ์ฒด ๋ฅผ +[ ์˜์—ญ 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 ๋ฉ”์„œ๋“œ๋Š” ์ฃผ๊ธฐ์  ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ƒ์œ„ ํ•ญ๋ชฉ์„ ์ง์ ‘ ๋˜๋Š” ๊ฐ„์ ‘์ ์œผ๋กœ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด์™€ ๊ด€๋ จ๋œ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

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

CRUD