RKConnectionDescription Class Reference
Inherits from | NSObject |
Conforms to | NSCopying |
Declared in | RKConnectionDescription.h |
Overview
The RKConnectionDescription
class describes a means for connecting a Core Data relationship. Connections can be established either by foreign key, in which case one or more attribute values on the source entity correspond to matching values on the destination entity, or by key path, in which case a key path is evaluated on the object graph to obtain a value for the relationship. Connection objects are used by instances of RKRelationshipConnectionOperation
to connect a relationship of a given managed object.
Foreign Key Connections
A foreign key connection is established by identifying managed objects within a context which have corresponding values on the source and destination objects. This is typically used to model relationships in the same way one would within a relational database.
For example, consider the example of a User
entity that has a to-many relationship named ‘projects’ for the Project
entity. Within the User
entity, there is an attribute named ‘userID’ that models the value for a given user’s primary key as provided to the application by the remote backend API with which it is communicating. Within the Project
entity, a corresponding ‘userID’ attribute exists specifying the value of the primary key for the User
that owns the project. The applications loads each of these object representations independently from the ‘/me/profile’ and ‘/projects’ resources. The JSON representation returned for a given Project
entity looks something like:
{ "project":
{ "id": 12345,
"name": "My Project",
"userID": 1
}
}
When this representation is mapped to a managed object for the Project
entity, the ‘user’ relationship cannot be mapped directly because there is no nested representation — only the primary key is available. In this case, the relationship can be connected by describing the association between the entities with an RKConnectionDescription
object:
NSEntityDescription *projectEntity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *userRelationship = [projectEntity relationshipsByName][@"user"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:relationship attributes:@{ @"userID": @"userID" }];
Note that the value for the attributes
argument is provided as a dictionary. Each pair within the dictionary correspond to an attribute pair in which the key is an attribute on the source entity (in this case, the Project
) and the value is the destination entity (in this case, the User
).
Any number of attribute pairs may be specified, but all values must match for the connection to be satisfied and the relationship’s value to be set.
Key Path Connections
A key path connection is established by evaluating the key path of the connection against the managed object being connected. The returned value has type transformation applied and is then assigned to the relationship.
Tasks
Connecting Relationships by Foreign Keys
-
– initWithRelationship:attributes:
Initializes the receiver with a given relationship and a dictionary of attributes specifying how to connect the relationship.
-
attributes
The dictionary of attributes specifying how attributes on the source entity for the relationship correspond to attributes on the destination entity.
property -
– isForeignKeyConnection
Returns a Boolean value indicating if the receiver describes a foreign key connection.
Connecting Relationships by Key Path
-
– initWithRelationship:keyPath:
Initializes the receiver with a given relationship and key path.
-
keyPath
The key path that is to be evaluated to obtain the value for the relationship.
property -
– isKeyPathConnection
Returns a Boolean value indicating if the receiver describes a key path connection.
Accessing the Relationship to be Connected
-
relationship
Returns the relationship that is to be connected.
property
Setting the Predicate
-
predicate
An optional predicate for filtering objects to be connected.
property
Properties
attributes
The dictionary of attributes specifying how attributes on the source entity for the relationship correspond to attributes on the destination entity.
@property (nonatomic, copy, readonly) NSDictionary *attributes
Discussion
This attribute is nil
unless the value of isForeignKeyConnection
is YES
.
Declared In
RKConnectionDescription.h
keyPath
The key path that is to be evaluated to obtain the value for the relationship.
@property (nonatomic, copy, readonly) NSString *keyPath
Discussion
This attribute is nil
unless the value of isKeyPathConnection
is YES
.
Declared In
RKConnectionDescription.h
Instance Methods
initWithRelationship:attributes:
Initializes the receiver with a given relationship and a dictionary of attributes specifying how to connect the relationship.
- (id)initWithRelationship:(NSRelationshipDescription *)relationship attributes:(NSDictionary *)sourceToDestinationEntityAttributes
Parameters
- relationship
The relationship to be connected.
- sourceToDestinationEntityAttributes
A dictionary specifying how attributes on the source entity correspond to attributes on the destination entity.
Return Value
The receiver, initialized with the given relationship and attributes.
Declared In
RKConnectionDescription.h
initWithRelationship:keyPath:
Initializes the receiver with a given relationship and key path.
- (id)initWithRelationship:(NSRelationshipDescription *)relationship keyPath:(NSString *)keyPath
Parameters
- relationship
The relationship to be connected.
- keyPath
The key path from which to read the value that is to be set for the relationship.
Return Value
The receiver, initialized with the given relationship and key path.
Declared In
RKConnectionDescription.h