Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Source/MMRecord/MMRecordRepresentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@
*/
- (NSString *)primaryKeyPropertyName;

/**
This method is responsible for determining the name of the primary key property given the entity
description for this instance of an MMRecordRepresentation.
@param entity The entity description for this representation.
@return The name of the primary key property for this entity.
@discussion This method is designed to be subclassed if the user wishes to provide a different way
of determining which property to use as the primary key for a given type of entity.
@warning This method is called once per entity or superentity until a primary key is found, or no
further superentity exists for a given entity. It is in this way that this class supports entity
inheritance, meaning that if no primary key property is designated on an entity then the class will
go through the chain of superentities to see if a primary key exists there.
*/
- (NSString *)primaryKeyPropertyNameForEntityDescription:(NSEntityDescription *)entity;

/**
This method returns the primary key attribute description for this entity. This method will return
nil if the entity uses a relationship for its primary key.
Expand Down
36 changes: 22 additions & 14 deletions Source/MMRecord/MMRecordRepresentation.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ - (instancetype)initWithEntity:(NSEntityDescription *)entity {
_attributeRepresentations = [NSMutableArray array];
_relationshipRepresentations = [NSMutableArray array];
_recordClassDateFormatter = [NSClassFromString([entity managedObjectClassName]) dateFormatter];
_primaryKey = [self primaryAttributeKeyForEntityDescription: entity];
_primaryKey = [self representationPrimaryKeyForEntityDescription:entity];
//TODO: Improve error handling for invalid primary key that may be returned from a subclass
// or be misconfigured in the model file.

[self createRepresentationMapping];
}
Expand Down Expand Up @@ -105,6 +107,25 @@ - (NSString *)primaryKeyPropertyName {
return self.primaryKey;
}

- (NSString *)primaryKeyPropertyNameForEntityDescription:(NSEntityDescription *)entity {
NSDictionary *userInfo = [entity userInfo];
NSString *primaryKeyPropertyName = [userInfo valueForKey:MMRecordEntityPrimaryAttributeKey];

return primaryKeyPropertyName;
}

- (NSString *)representationPrimaryKeyForEntityDescription:(NSEntityDescription *)entity {
NSString *primaryKeyPropertyName;
NSEntityDescription *currentEntity = entity;

while (!primaryKeyPropertyName && currentEntity) {
primaryKeyPropertyName = [self primaryKeyPropertyNameForEntityDescription:entity];
currentEntity = currentEntity.superentity;
}

return primaryKeyPropertyName;
}

- (NSAttributeDescription *)primaryAttributeDescription {
NSString *primaryKeyPropertyName = [self primaryKeyPropertyName];
id primaryKeyRepresentation = self.representationDictionary[primaryKeyPropertyName];
Expand All @@ -116,19 +137,6 @@ - (NSAttributeDescription *)primaryAttributeDescription {
return nil;
}

- (NSString *)primaryAttributeKeyForEntityDescription: (NSEntityDescription *)entity {
NSString *primaryKey;
NSEntityDescription *currentEntity = entity;

while (!primaryKey && currentEntity) {
NSDictionary *userInfo = [entity userInfo];
primaryKey = [userInfo valueForKey:MMRecordEntityPrimaryAttributeKey];
currentEntity = currentEntity.superentity;
}

return primaryKey;
}

- (id)primaryKeyValueFromDictionary:(NSDictionary *)dictionary {
id primaryKeyRepresentation = self.representationDictionary[self.primaryKey];

Expand Down