JSON body:
"returnCode":{“messages”[], "something":1}
Should be mapped to:
@interface ReturnCodeEntity : NSMutableObject
@property (nonatomic, retain) id messages; // Should be defined as Transformable?
@property (nonatomic, retain) NSString * something;
Hi, I guess these are two problems:
Now I get an exception after the mapping operation was finished:
2014-04-10 12:12:05.661 [4811:3f07] D restkit.object_mapping:RKMappingOperation.m:929 Finished mapping operation successfully...
2014-04-10 12:12:05.661 [4811:3f07] -[__NSDictionaryM _stateFlags]: unrecognized selector sent to instance 0x10990d0b0
2014-04-10 12:12:08.786 [4811:3f07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM _stateFlags]: unrecognized selector sent to instance 0x10990d0b0'
First throw call stack:
(
0 CoreFoundation 0x0000000102af8495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010285799e objc_exception_throw + 43
2 CoreFoundation 0x0000000102b8965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000102ae9d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000102ae9938 _CF_forwarding_prep_0 + 120
5 CoreData 0x00000001008ec8f8 -[NSRelationshipDescription(_NSInternalMethods) _nonPredicateValidateValue:forKey:inObject:error:] + 232
6 CoreData 0x00000001008eba27 -[NSManagedObject(_NSInternalMethods) _validateValue:forProperty:andKey:withIndex:error:] + 375
7 CoreData 0x00000001009651d7 -[NSManagedObject validateValue:forKey:error:] + 135
8 Foundation 0x0000000100f19ed5 -[NSObject(NSKeyValueCoding) validateValue:forKeyPath:error:] + 336
9 0x0000000100212cb5 -[RKMappingOperation validateValue:atKeyPath:] + 341
10 0x000000010021334d -[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:] + 861
11 0x00000001002177dd -[RKMappingOperation mapOneToOneRelationshipWithValue:mapping:] + 1661
12 0x000000010021b14a -[RKMappingOperation applyRelationshipMappings] + 6186
13 0x000000010021d2f6 -[RKMappingOperation main] + 4006
14 Foundation 0x0000000100f2bd34 -[__NSOperationInternal _start:] + 623
15 0x000000010020ca5e -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] + 1886
16 0x000000010020b3ff -[RKMapperOperation mapRepresentation:atKeyPath:usingMapping:] + 1871
17 0x000000010020dd75 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 805
18 0x000000010020e5a8 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 1944
19 0x000000010020eef5 -[RKMapperOperation main] + 1365
There is a discussion on groups.google.com but it ends up with a workaround.
Thank you for suggestions.
EDIT
I have introduced a typhoo in the body. Yes there was semicolon missing in this post
{"returnCode":{"messages":[], "something":1}}
I made some changes in the model so the ReturnCodeEntity contains NSSet referencing to new type MessageEntity which stores the message in the 'text' attribute:
@interface ReturnCodeEntity : NSMutableObject
@property (nonatomic, retain) NSString * something;
@property (nonatomic, retain) NSSet *messages; // --> relationship 1 to many MessageEntitie's
@end
@interface ReturnCodeEntity (CoreDataGeneratedAccessors)
- (void)addMessagesObject:(MessageEntity *)value;
- (void)removeMessagesObject:(MessageEntity *)value;
- (void)addMessages:(NSSet *)values;
- (void)removeMessages:(NSSet *)values;
@end
@interface MessageEntity : NSMutableObject
@property (nonatomic, retain) NSString * text;
@property (nonatomic, retain) ReturnCodeEntity *returnCode; // --> relationship 1 to 1 ReturnCodeEntity
@end
I have created mapping for MessageEntity:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"MessageEntity" inManagedObjectStore:managedObjectStore];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"text"]];
mapping.identificationAttributes = @[ @"text" ];
... and added it inside returnCodeEntityMapping
[returnCodeEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"messages" toKeyPath:@"messages" withMapping:messageApiMapping]];
The trace says the mappings were successful in the hierarchy but later it crashes in RKMappingOperation.m in the method *- (BOOL)validateValue:(id *)value atKeyPath:(NSString )keyPath
success = [self.destinationObject validateValue:value forKeyPath:keyPath error:&validationError];
Thank you Wain for your help.
My problem was the instantiation of RKObjectMapping for MessageEntity mapping instead of RKAttributeMapping.
It is because I have copied the code from another entity class that uses [RKObjectMapping requestMapping](because of requests). I must add it is confusing (from Restkit user's point of view) to use RKObjectMapping for requests even if under the hood I do not need to use RKAttributeMapping. I would prefer here consistent api (RKAttributeMapping). But anyway Restkit is a nice tool to avoid manual parsing/mapping.
This solution from Wain was useful