Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Datos del modelo

Model Relationships - Swift SDK

Tip

Alternativamente, puedes Define tus relaciones en tu aplicación App Services.

A to-one relationship maps one property to a single instance of another object type. For example, you can model a person having at most one companion dog as a to-one relationship.

Si se configura un campo de relación como nulo, se remueve la conexión entre objetos. De todas maneras, Realm no elimina el objeto referenciado, a menos que sea un objeto incrustado.

Importante

To-one relationships must be optional

When you declare a to-one relationship in your object model, it must be an optional property. If you try to make a to-one relationship required, Realm throws an exception at runtime.

// Dog.h
@interface Dog : RLMObject
@property NSString *name;
// No backlink to person -- one-directional relationship
@end
// Define an RLMArray<Dog> type
RLM_COLLECTION_TYPE(Dog)
// Person.h
@interface Person : RLMObject
@property NSString *name;
// A person can have one dog
@property Dog *dog;
@end
// Dog.m
@implementation Dog
@end
// Person.m
@implementation Person
@end
class Person: Object {
@Persisted var name: String = ""
@Persisted var birthdate: Date = Date(timeIntervalSince1970: 1)
// A person can have one dog
@Persisted var dog: Dog?
}
class Dog: Object {
@Persisted var name: String = ""
@Persisted var age: Int = 0
@Persisted var breed: String?
// No backlink to person -- one-directional relationship
}

Tip

Para obtener más información sobre las relaciones de uno a uno, consulte: Relación de uno a uno.

Si tu aplicación utiliza Device Sync, consulta la página Model Data with Device Sync para obtener información sobre cómo se traduce la relación uno a uno en los modelos de objetos Swift a documentos de Atlas.

Una relación uno a muchos asigna una propiedad a cero o más instancias de otro tipo de objeto Realm. Por ejemplo, puedes modelar a una persona que tiene cualquier número de perros acompañantes como una relación de uno a muchos.

Utiliza RLMArray etiquetado con el tipo de tu objetivo para definir tu propiedad de relación de uno a muchos.

Tip

Recuerde utilizar el RLM_COLLECTION_TYPE() macro con su tipo para declarar el protocolo RLMArray para su tipo.

// Dog.h
@interface Dog : RLMObject
@property NSString *name;
// No backlink to person -- one-directional relationship
@end
// Define an RLMArray<Dog> type
RLM_COLLECTION_TYPE(Dog)
// Person.h
@interface Person : RLMObject
@property NSString *name;
// A person can have many dogs
@property RLMArray<Dog *><Dog> *dogs;
@end
// Dog.m
@implementation Dog
@end
// Person.m
@implementation Person
@end

Use List tagged with your target type to define your to-many relationship property.

class Person: Object {
@Persisted var name: String = ""
@Persisted var birthdate: Date = Date(timeIntervalSince1970: 1)
// A person can have many dogs
@Persisted var dogs: List<Dog>
}
class Dog: Object {
@Persisted var name: String = ""
@Persisted var age: Int = 0
@Persisted var breed: String?
// No backlink to person -- one-directional relationship
}

Tip

Para obtener más información sobre las relaciones de muchos, consulte: Relación de muchos.

If your app uses Device Sync, see the Model Data with Device Sync page for information on how the to-many relationship in Swift object models translates to Atlas documents.

An inverse relationship property is an automatic backlink relationship. Realm automatically updates implicit relationships whenever an object is added or removed in a corresponding to-many list or to-one relationship property. You cannot manually set the value of an inverse relationship property.

To define an inverse relationship, use LinkingObjects in your object model. The LinkingObjects definition specifies the object type and property name of the relationship that it inverts.

class User: Object {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var _partition: String = ""
@Persisted var name: String = ""
// A user can have many tasks.
@Persisted var tasks: List<Task>
}
class Task: Object {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var _partition: String = ""
@Persisted var text: String = ""
// Backlink to the user. This is automatically updated whenever
// this task is added to or removed from a user's task list.
@Persisted(originProperty: "tasks") var assignee: LinkingObjects<User>
}

Para definir una relación inversa, usa RLMLinkingObjects en tu modelo de objetos. Reemplaza el método +[RLMObject linkingObjectProperties] en tu clase para especificar el tipo de objeto Realm y el nombre de la propiedad de la relación que invierte.

// Task.h
@interface Task : RLMObject
@property NSString *description;
@property (readonly) RLMLinkingObjects *assignees;
@end
// Define an RLMArray<Task> type
RLM_COLLECTION_TYPE(Task)
// User.h
@interface User : RLMObject
@property NSString *name;
@property RLMArray<Task *><Task> *tasks;
@end
// Task.m
@implementation Task
+ (NSDictionary *)linkingObjectsProperties {
return @{
@"assignees": [RLMPropertyDescriptor descriptorWithClass:User.class propertyName:@"tasks"],
};
}
@end
// User.m
@implementation User
@end

To define an inverse relationship, use LinkingObjects in your object model. The LinkingObjects definition specifies the object type and property name of the relationship that it inverts.

class User: Object {
@objc dynamic var _id: ObjectId = ObjectId.generate()
@objc dynamic var _partition: String = ""
@objc dynamic var name: String = ""
// A user can have many tasks.
let tasks = List<Task>()
override static func primaryKey() -> String? {
return "_id"
}
}
class Task: Object {
@objc dynamic var _id: ObjectId = ObjectId.generate()
@objc dynamic var _partition: String = ""
@objc dynamic var text: String = ""
// Backlink to the user. This is automatically updated whenever
// this task is added to or removed from a user's task list.
let assignee = LinkingObjects(fromType: User.self, property: "tasks")
override static func primaryKey() -> String? {
return "_id"
}
}

Tip

For more information about inverse relationships, see: Inverse Relationship.

If your app uses Device Sync, see the Model Data with Device Sync page for information on how the inverse relationship in Swift object models translates to Atlas documents.

Un objeto incrustado existe como datos anidados dentro de un único objeto padre específico. Hereda el ciclo de vida de su objeto padre y no puede existir como un objeto independiente de Realm. Realm elimina automáticamente los objetos incrustados si su objeto padre es eliminado o cuando es sobrescrito por una nueva instancia de objeto incrustado.

Nota

Realm Uses Cascading Deletes for Embedded Objects

When you delete a Realm object, any embedded objects referenced by that object are deleted with it. If you want the referenced objects to persist after the deletion of the parent object, your type should not be an embedded object at all. Use a regular Realm object with a to-one relationship instead.

You can define an embedded object by deriving from the RLMEmbeddedObject class. You can use your embedded object in another model as you would any other type.

// Define an embedded object
@interface Address : RLMEmbeddedObject
@property NSString *street;
@property NSString *city;
@property NSString *country;
@property NSString *postalCode;
@end
// Enable Address for use in RLMArray
RLM_COLLECTION_TYPE(Address)
@implementation Address
@end
// Define an object with one embedded object
@interface Contact : RLMObject
@property NSString *name;
// Embed a single object.
@property Address *address;
@end
@implementation Contact
@end
// Define an object with an array of embedded objects
@interface Business : RLMObject
@property NSString *name;
// Embed an array of objects
@property RLMArray<Address *><Address> *addresses;
@end

You can define an embedded object by deriving from the EmbeddedObject class. You can use your embedded object in another model as you would any other type.

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>
// Inverse relationship - a person can be a member of many clubs
@Persisted(originProperty: "members") var clubs: LinkingObjects<DogClub>
// 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 DogClub: Object {
@Persisted var name = ""
@Persisted var members: List<Person>
// DogClub has an array of regional office addresses.
// These are embedded objects.
@Persisted var regionalOfficeAddresses: List<Address>
convenience init(name: String, addresses: [Address]) {
self.init()
self.name = name
self.regionalOfficeAddresses.append(objectsIn: addresses)
}
}
class Address: EmbeddedObject {
@Persisted var street: String?
@Persisted var city: String?
@Persisted var country: String?
@Persisted var postalCode: String?
}

Tip

If your app uses Device Sync, see the Model Data with Device Sync page for information on how embedded objects in Swift object models translate to Atlas documents.

Volver

Define un modelo de objeto

En esta página