Skip to content

Commit

Permalink
Fixed a duplication bug with relationship primary keys within nested …
Browse files Browse the repository at this point in the history
…level records
  • Loading branch information
Conrad Stoll committed Feb 27, 2014
1 parent f9266fa commit 9de83c7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "Post.h"
#import "PostCell.h"
#import "ADNPostManager.h"
#import "Counts.h"

@interface ADNPostsViewController ()

Expand All @@ -28,10 +29,13 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(getMoreRecentPosts) forControlEvents:UIControlEventValueChanged];
[self.refreshControl addTarget:self
action:@selector(getMoreRecentPosts)
forControlEvents:UIControlEventValueChanged];
[self setRefreshControl:self.refreshControl];

[self.tableView registerNib:[UINib nibWithNibName:@"PostCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"PostCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"PostCell" bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"PostCell"];

[self getPosts];
}
Expand All @@ -43,12 +47,42 @@ - (void)getPosts {
NSManagedObjectContext *context = [[MMDataManager sharedDataManager] managedObjectContext];

MMRecordOptions *options = [Post defaultOptions];
options.entityPrimaryKeyInjectionBlock = ^id(NSEntityDescription *entity, NSDictionary *dictionary){
return [dictionary valueForKey:@"url"];

options.entityPrimaryKeyInjectionBlock = ^id(NSEntityDescription *entity,
NSDictionary *dictionary,
MMRecordProtoRecord *parentProtoRecord){
if ([[entity name] isEqualToString:@"CoverImage"]) {
if ([[parentProtoRecord.entity name] isEqualToString:@"User"]) {
if (parentProtoRecord.primaryKeyValue != nil) {
return parentProtoRecord.primaryKeyValue;
}
}
}

return nil;
};

options.recordPrePopulationBlock = ^(MMRecordProtoRecord *protoRecord){
if (protoRecord.primaryKeyValue) {
NSLog(@"Entity: %@ Value Key: %@", protoRecord.entity.name, protoRecord.primaryKeyValue);
} else {
NSLog(@"Entity: %@ Relationship Key: %@", protoRecord.entity.name, protoRecord.relationshipPrimaryKeyProto.primaryKeyValue);
}
};

options.deleteOrphanedRecordBlock = ^BOOL(MMRecord *orphan,
NSArray *populatedRecords,
id responseObject,
BOOL *stop) {
if ([orphan isKindOfClass:[Counts class]]) {
if ([(Counts *)orphan user] == nil) {
return YES;
}
}

return NO;
};

[Post setOptions:options];

[Post
Expand Down
5 changes: 4 additions & 1 deletion Source/MMRecord/MMRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,13 @@ typedef BOOL (^MMRecordOptionsDeleteOrphanedRecordBlock)(MMRecord *orphan,
@param entity The entity type to evaluate and return a primary key for.
@param dictionary The dictionary being used to populate the given record.
@param parentProtoRecord The parent proto record of the one whose primary key is being evaluated
here. This may be nil if the entity is the initial entity being populated by MMRecord.
@return id The primary key to associate with the record.
*/
typedef id<NSCopying> (^MMRecordOptionsEntityPrimaryKeyInjectionBlock)(NSEntityDescription *entity,
NSDictionary *dictionary);
NSDictionary *dictionary,
MMRecordProtoRecord *parentProtoRecord);

/**
This block may be used for inserting custom logic into the record population workflow. This block,
Expand Down
3 changes: 3 additions & 0 deletions Source/MMRecord/MMRecordProtoRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
- (void)addRelationshipProto:(MMRecordProtoRecord *)relationshipProto
forRelationshipDescription:(NSRelationshipDescription *)relationshipDescription;

// Returns YES if there is already a valid relationshipProtoRecord for a given relationshipDescription
- (BOOL)canAccomodateAdditionalProtoRecordForRelationshipDescription:(NSRelationshipDescription *)relationshipDescription;

// Returns the proto records for a given relationship description
- (NSArray *)relationshipProtoRecordsForRelationshipDescription:(NSRelationshipDescription *)relationshipDescription;

Expand Down
15 changes: 12 additions & 3 deletions Source/MMRecord/MMRecordProtoRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ - (NSArray *)relationshipProtoRecordsForRelationshipDescription:(NSRelationshipD
return relationshipProtoRecords;
}

- (BOOL)canAccomodateAdditionalProtoRecordForRelationshipDescription:(NSRelationshipDescription *)relationshipDescription {
NSString *relationshipName = [relationshipDescription name];
NSMutableOrderedSet *protoSet = [self.relationshipProtosDictionary objectForKey:relationshipName];

if ([relationshipDescription isToMany] == NO && [protoSet count] >= 1) {
return NO;
}

return YES;
}


#pragma mark - Relationships

Expand All @@ -98,9 +109,7 @@ - (void)addRelationshipProto:(MMRecordProtoRecord *)relationshipProto
protoSet = [NSMutableOrderedSet orderedSet];
}

if ([relationshipDescription isToMany] == NO && [protoSet count] >= 1) {
// Ignore this relationshipProto because its a duplicate
} else {
if ([self canAccomodateAdditionalProtoRecordForRelationshipDescription:relationshipDescription]) {
[protoSet addObject:relationshipProto];
}

Expand Down
23 changes: 15 additions & 8 deletions Source/MMRecord/MMRecordResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ - (void)buildProtoRecordsAndResponseGroups {

MMRecordProtoRecord *proto = [self protoRecordWithRecordResponseObject:recordResponseObject
entity:entity
existingResponseGroups:responseGroups];
existingResponseGroups:responseGroups
parentProtoRecord:nil];

[objectGraph addObject:proto];
}
Expand All @@ -199,7 +200,8 @@ - (void)buildProtoRecordsAndResponseGroups {

- (MMRecordProtoRecord *)protoRecordWithRecordResponseObject:(id)recordResponseObject
entity:(NSEntityDescription *)entity
existingResponseGroups:(NSMutableDictionary *)responseGroups {
existingResponseGroups:(NSMutableDictionary *)responseGroups
parentProtoRecord:(MMRecordProtoRecord *)parentProtoRecord {
MMRecordResponseGroup *recordResponseGroup = [self responseGroupForEntity:entity
fromExistingResponseGroups:responseGroups];
MMRecordRepresentation *representation = recordResponseGroup.representation;
Expand All @@ -220,7 +222,8 @@ - (MMRecordProtoRecord *)protoRecordWithRecordResponseObject:(id)recordResponseO
if (proto.primaryKeyValue == nil) {
if (self.entityPrimaryKeyInjectionBlock != nil) {
proto.primaryKeyValue = self.entityPrimaryKeyInjectionBlock(proto.entity,
proto.dictionary);
proto.dictionary,
parentProtoRecord);
}
}
}
Expand Down Expand Up @@ -273,11 +276,15 @@ - (void)addRelationshipProtoRecordsToProtoRecord:(MMRecordProtoRecord *)protoRec
}

for (id object in relationshipObject) {
MMRecordProtoRecord *relationshipProto = [self protoRecordWithRecordResponseObject:object
entity:entity
existingResponseGroups:responseGroups];

[protoRecord addRelationshipProto:relationshipProto forRelationshipDescription:relationshipDescription];
if ([protoRecord canAccomodateAdditionalProtoRecordForRelationshipDescription:relationshipDescription]) {
MMRecordProtoRecord *relationshipProto = [self protoRecordWithRecordResponseObject:object
entity:entity
existingResponseGroups:responseGroups
parentProtoRecord:protoRecord];

[protoRecord addRelationshipProto:relationshipProto
forRelationshipDescription:relationshipDescription];
}
}
}
}
Expand Down

0 comments on commit 9de83c7

Please sign in to comment.