2
votes

So after reading this answer, I do not see how my case is any different, but the proposed solution just doesn't seem to work in RestKit 0.20.3

For the following json:

{
   "photos": [
      "872ac577-3f31-47a0-966e-6f2ed2fbabcd"
     ],
   "imageUrl": [
      "http://domain/WebAPI/FileUpload/10232013629_21382571406.439790.jpeg"
     ]
}

Mapping into the following object:

@class Picture, Site, Survey;

@interface Job : NSManagedObject

@property (nonatomic, retain) NSString * jobId;
@property (nonatomic, retain) NSString * jobName;
@property (nonatomic, retain) NSString * jobStatusId;
@property (nonatomic, retain) NSString * jobStatus;
@property (nonatomic, retain) NSString * projectId;
@property (nonatomic, retain) NSString * projectType;
@property (nonatomic, retain) NSString * siteId;
@property (nonatomic, retain) NSSet *surveys;
@property (nonatomic, retain) NSSet *photos;
@property (nonatomic, retain) NSDate *createdDate;
@property (nonatomic, retain) Site *site;
@end

Where photos is a Picture object that looks like this:

@interface Picture : NSManagedObject
@property (nonatomic, retain) NSData * data;
@property (nonatomic, retain) NSString * photoId;
@property (nonatomic, retain) NSString * pictureLoc;
@property (nonatomic, retain) NSString * imageUrl;
@property (nonatomic, retain) Job *job;
@property (nonatomic, retain) QuestionAnswer *question;
@end

I want to map the photos and imageUrl into the Picture's photoId and imageUrl, respectively. I thought I could use the following in my Job's mapping:

RKEntityMapping *ret = [RKEntityMapping mappingForEntityForName:@"Job" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];

[ret addAttributeMappingsFromDictionary: @{
                                           @"photos": @"photos.photoId",
                                           @"imageUrl": @"photos.imageUrl",
                                           }];

However, the mapped Job always ends up with nothing in the photos property.

I know that I am doing all the in-between steps correctly (like actually using ret for the mapping), as the JSON is a snippet that contains many other keys I am able to map with no problem. What am I doing wrong? please tell me

EDIT

Changed the mapping code as per Wain's answer:

RKEntityMapping *ret = [RKEntityMapping mappingForEntityForName:@"Job" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];
RKEntityMapping *subMapping = [RKEntityMapping mappingForEntityForName:@"Picture" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore ];

[subMapping addAttributeMappingsFromDictionary: @{
                                           @"photos":@"photoId",
                                           @"imageUrl":@"imageUrl"
                                           }];


[ret addPropertyMapping:[RKRelationshipMapping
                         relationshipMappingFromKeyPath:nil 
                         toKeyPath:@"photos" 
                         withMapping:subMapping]];

And now I get a Picture object added, but with nil for photoId and imageUrl (and yes, I checked to make sure I have the spelling and capitalization correct for the JSON keys)

1

1 Answers

0
votes

The reason photos never has anything in it is that your mapping doesn't tell it to create a new object.

The problem is how you could tell it to create a single object and set both of the parameters into it. The problem is because you have 2 different arrays. The JSON I would expect you to have is 1 array which contains dictionaries with all of the details for that particular entry. RestKit can process both of the arrays but it would result in 2 distinct photo objects being created. To do this you need a second mapping with a nil key path and a relationship from ret to that new mapping.

An alternative could be to change the Job object so that it has 2 transformable (or transient) properties which are NSArrays. If you set ret to just map @"photos" : @"photos" and @"imageUrl" : @"imageUrl" then RestKit will map all of the data, just not into a photo object. You can then mutate this data after the mapping has completed if you want to...