0

If I have a JSON view that looks like this for a user object in my API:

{
    "id": 1,
    "posts": [
        { "id": 102 },
        { "id": 101 },
        { "id": 100 }
    ]
}

There are never more than three items in the posts array, it is supposed to represent the three most recent. However, if more posts, like { "id": 99 } have already been downloaded, RestKit will assume that they no longer exist and that only the three posts in the array on the user view are still around.

How can I make RestKit merge the newly-loaded posts with the currently loaded posts, rather than just deleting older ones to make room for the new?

I'm also open to changing my JSON if there's a better way to do this.

These are my mappings:

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@{
    @"avatar_url": @"avatarURL",
    @"id":         @"userID",
    @"first_name": @"firstName",
    @"last_name":  @"lastName",
    @"username":   @"username",
    @"followed":   @"followed"
}];
userMapping.identificationAttributes = @[@"userID"];

RKEntityMapping *postMapping = [RKEntityMapping mappingForEntityForName:@"Post" inManagedObjectStore:managedObjectStore];
[postMapping addAttributeMappingsFromDictionary:@{
    @"id":          @"postID",
    @"content_url": @"contentURL",
    @"created_at":  @"createdAt"
}];
postMapping.identificationAttributes = @[@"postID"];

[userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"posts"    toKeyPath:@"posts"   withMapping:postMapping]];
[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"creator"  toKeyPath:@"creator" withMapping:userMapping]];

[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"replies"  toKeyPath:@"replies" withMapping:postMapping]];
[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"reply_to" toKeyPath:@"replyTo" withMapping:postMapping]];
4

1 回答 1

0

Try this to build the relationship mapping:

RKRelationshipMapping *postRelationMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"posts" toKeyPath:@"posts" withMapping:postMapping]
postRelationMapping.assignmentPolicy = RKUnionAssignmentPolicy;

[userMapping addPropertyMapping:postRelationMapping];
于 2013-06-08T10:33:17.947 回答