logo
down
shadow

RestKit mapping with parent key as attribute and key contains parenthesis


RestKit mapping with parent key as attribute and key contains parenthesis

By : N1troGn
Date : November 18 2020, 03:01 PM
help you fix your problem Solution:
Update the RKStringByReplacingUnderscoresWithBraces method in RKPropertyMapping to not replace the parenthesis with braces and then be sure to use braces in your attribute mappings.
code :
// RKPropertyMapping
static NSString *RKStringByReplacingUnderscoresWithBraces(NSString *string)
{
    return string;
    //return [[string stringByReplacingOccurrencesOfString:@"(" withString:@"{"] stringByReplacingOccurrencesOfString:@")" withString:@"}"];
}


// Attribute Mappings
[mapping addAttributeMappingsFromDictionary:@{
    @"{name}.param_1":@"param1",
    @"{name}.param_2":@"param2"
}];
// JSON   
{ "blake": {
    "email": "blake@restkit.org",
    "favorite_animal": "Monkey"
    }
}

// Class
@interface User : NSObject
@property (nonatomic, copy) NSString* email
@property (nonatomic, copy) NSString* username;
@property (nonatomic, copy) NSString* favoriteAnimal;
@end

// Mapping
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ];
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"username"];
[mapping addAttributeMappingsFromDictionary:@{
    @"(username).email": @"email",
    @"(username).favorite_animal": @"favoriteAnimal"
}];
// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"


// 1. The attribute mapping has been performed for 'blake' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
      sourceKeyPath = @"{username}.email"
      destinationKeyPath = @"email"

      * replaces values *
      sourceKeyPath = @"blake.email"
      destinationKeyPath = @"email"

      * creates a new RKPropertyMapping object *
      RKPropertyMapping
        - sourceKeyPath = @"blake.email"
        - destinationKeyPath = @"email"
// JSON   
{ "blake (a.k.a GOAT)": {
    "email": "blake@restkit.org",
    "favorite_animal": "Monkey"
    }
}

// Resulting Attribute Mappings
@"{username}.email": @"email",
@"{username}.favorite_animal": @"favoriteAnimal"


// 1. The attribute mapping has been performed for 'blake (a.k.a GOAT)' > 'username'
// 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake (a.k.a GOAT)' so your further nested attribute mappings can take place
// 3. Example: @"{username}.email": @"email"
      sourceKeyPath = @"{username}.email"
      destinationKeyPath = @"email"

      * replaces values *
      sourceKeyPath = @"blake (a.k.a GOAT).email"
      destinationKeyPath = @"email"

      * creates a new RKPropertyMapping object *
      RKPropertyMapping
        - sourceKeyPath = @"blake {a.k.a GOAT}.email" // DOES NOT MATCH
        - destinationKeyPath = @"email"


Share : facebook icon twitter icon
Restkit - Child mapping doesn't inherit its parent mapping?

Restkit - Child mapping doesn't inherit its parent mapping?


By : legend10
Date : March 29 2020, 07:55 AM
I wish this helpful for you In short, it doesn't work because RestKit doesn't support it, and in my opinion shouldn't.
If you want the child mapping to have the parent mapping you can just add it to the child mapping too, which is a single additional line of code if you write them independently, or just a few extra characters if you use the mapKeyPathsToAttributes: method.
code :
RKManagedObjectMapping* parentMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Child" inManagedObjectStore:store];
parentMapping.primaryKeyAttribute = @"parentID";
[parentMapping mapKeyPathsToAttributes: @"id", @"parentID", @"property_one", @"propertyOne", @"parent_only_property", @"parentOnlyProperty"];
[[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:@"parent"];

RKManagedObjectMapping* childMapping = [parentMapping copy];
childMapping.primaryKeyAttribute = @"childID";
[childMapping removeMapping:[childMapping mappingForAttribute:@"parentID"]];
[childMapping removeMapping:[childMapping mappingForAttribute:@"parentOnlyProperty"];
[childMapping mapKeyPathsToAttributes:@"id", @"childID", @"child_only_property", @"childOnlyProperty"];
[[RKObjectManager sharedManager].mappingProvider setMapping:childMapping forKeyPath:@"child"];
RestKit @root @parent accessors for mapping

RestKit @root @parent accessors for mapping


By : TNEG
Date : March 29 2020, 07:55 AM
wish helps you I think the problem is the key paths you're using in the response descriptors for the mappings where you try to use @root. When you specify a keypath on the response descriptor you are effectively changing the @root object because you are diving into the content to a specified depth and that depth becomes the root for that mapping. If you debug during the mapping process and look at the supplied metadata you should be able to see / verify this.
I'm not clear on why you have so many different response descriptors. It would seem more logical to have one response descriptor for locations whose mapping defines all of the relationships between all of the different parts (and their mappings). Working in this way you would have much simpler code and you would also have access to @root / @parent.
RestKit XML Mapping with same element and attribute name

RestKit XML Mapping with same element and attribute name


By : KERRIE HAWKINS
Date : March 29 2020, 07:55 AM
around this issue As I already posted I now also tried RKXMLReaderSerialization and the problem was gone. The solution code-wise was changing
code :
[RKMIMETypeSerialization registerClass:[RKTBXMLSerialization class] forMIMEType:@"application/xml"];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
iOS RestKit - mapping results, just one attribute

iOS RestKit - mapping results, just one attribute


By : Spencer Carlson
Date : March 29 2020, 07:55 AM
I hope this helps you . Remove you usage of addAttributeMappingsFromDictionary:, and change to:
code :
[isFavoriteMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"Is_favorited"]];
Mapping JSON with RestKit without attribute names

Mapping JSON with RestKit without attribute names


By : karatebob
Date : March 29 2020, 07:55 AM
I hope this helps you . Using RestKit I am having trouble mapping the following JSON: As you see, the languages attribute/relationship does not contain any key information of the language entity. , This Mapping solved it:
code :
let rootMap = RKEntityMapping(forEntityForName: "RootData", inManagedObjectStore: store)
let rootDict = ["data.version": "version",] as [NSObject: AnyObject]!
rootMap.addAttributeMappingsFromDictionary(rootDict)

let languagesMap = RKEntityMapping(forEntityForName: "Language", inManagedObjectStore: store)


languagesMap.forceCollectionMapping = true      // thanks @Wain
//
// the following solved my problem regarding the mapping:
// - Key not present: * map from representation
//                    * reference identifier in name-mapping
// - Add identification attribute to keep them unique
//
let languageDict = ["(identifier)": "name",] as [NSObject : AnyObject]!    
languagesMap.addAttributeMappingsFromDictionary(languageDict)
languagesMap.identificationAttributes = ["identifier"]
languagesMap.addAttributeMappingFromKeyOfRepresentationToAttribute("identifier")

rootMap.addPropertyMapping(
    RKRelationshipMapping(
        fromKeyPath: "data.languages",
        toKeyPath: "languages", 
        withMapping: languagesMap)
    )
shadow
Privacy Policy - Terms - Contact Us © voile276.org