Sullivan
Sullivan

Reputation: 111

RestKit: How to POST a NSManagedObject as JSON without any nesting attributes?

This should be a really easy one, but sadly I haven't found any answer...

What RestKit mapped my object to:

request.body={"user":{"pass":"1234","id":0,"login":"awesome_guy","tier":0}}

What I really want:

{"pass":"1234","id":0,"login":"awesome_guy","tier":0}

Just without the object name "user".

If you have dealt with this issue, it'll take you 5 seconds to answer. If you haven't used RestKit. You do not know the answer. I'm attaching my code anyway:

User Object Mapping:

/* ===========================
 * ====User Object Mapping====
 * ==========================*/

RKEntityMapping* userObjectMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([User class]) inManagedObjectStore:objectManager.managedObjectStore];

NSDictionary *userObjectMappingDict = @{
                                    @"id":@"id",
                                    @"login":@"login",
                                    @"firstName":@"firstName",
                                    @"lastName":@"lastName",
                                    @"phoneNumber":@"phoneNumber",
                                    @"email":@"email",
                                    @"tier":@"tier",
                                    @"sessionId":@"sessionId",
                                    @"pass":@"password"
                                    };

userObjectMapping.identificationAttributes = @[@"id"];

[userObjectMapping addAttributeMappingsFromDictionary:userObjectMappingDict];

RKEntityMapping* userBusinessMapping =  [RKEntityMapping mappingForEntityForName:@"Business" inManagedObjectStore:objectManager.managedObjectStore];
[userBusinessMapping addAttributeMappingsFromDictionary:@{@"business":@"id"}]; // Nil Key path

[userObjectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"business" withMapping:userBusinessMapping]];

Upvotes: 0

Views: 268

Answers (1)

Wain
Wain

Reputation: 119031

You should have an instance of RKRequestDescriptor that you haven't shown. It's created with requestDescriptorWithMapping:objectClass:rootKeyPath:. You have the root key path set to @"user" and you should set it to nil.

Upvotes: 1

Related Questions