I'm struggling to even get basic stuff working in restkit. I am an experienced developer too which makes it even more frustrating.
I have successfully have it posting a json object to my service and via debugging through the RestKit source I have verified that the desired json has been returned. It returns to the success block but with nothing except an empty object.
Here is my code that sets up the mappings and launches the request.
Note: I have obscured some sensitive passwords/urls.
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"DeviceKey": @"DeviceKey", @"DeviceName": @"DeviceName", @"Password": @"Password", @"Username": @"Username"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[LoginRequest class]
rootKeyPath:nil];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
responseMapping.forceCollectionMapping = YES;
[responseMapping addAttributeMappingsFromDictionary:@{@"TotalBalance":@"TotalBalance",@"FirstName":@"FirstName"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:@"Login"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://api.test.com/account/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
manager.requestSerializationMIMEType = RKMIMETypeJSON;
LoginRequest *request = [[LoginRequest alloc] init];
request.DeviceKey = @"xxxxxxx";
request.DeviceName = @"iPhone";
request.Username = @"mike";
request.Password = @"******";
[manager postObject:request path:@"Login" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSArray * array = [result array];
NSDictionary * dictionary = [result dictionary];
NSObject * obj = [result firstObject];
NSLog(@"We object mapped the response with the following result: %@", result);
// It ends up in the success callback but all of these are EMPTY :(
} failure:^(RKObjectRequestOperation *operation, NSError *err){
NSLog(@"We had a problemo");
}];
LogonResponse.h (the .m simply synthisizes them properties)
#import <Foundation/Foundation.h>
@interface LoginResponse : NSObject
@property(nonatomic,assign) double TotalBalance;
@property(nonatomic,retain) NSString * FirstName;
@end
The JSON:
{
"TotalBalance":22.34,
"FirstName":"Mike"
........ other stuff but this shouldn't matter
}
The console is showing:
2013-06-26 14:03:07.173 Tests.com[1666:907] I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json
2013-06-26 14:03:07.181 Tests.com[1666:3907] T restkit.network:RKHTTPRequestOperation.m:139 POST 'http://api.test.com/account/Login':
request.headers={
Accept = "application/json";
"Accept-Language" = "en-GB, en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, ca, hu, vi, en-us;q=0.8";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Tests.com/2.10.31_dev (iPad; iOS 6.1.3; Scale/1.00)";
}
request.body={"DeviceKey":"xxxxxxx","DeviceName":"iPhone","Password":"******","Username":"mike"}
2013-06-26 14:03:07.297 Tests.com[1666:162f] D restkit.network:RKHTTPRequestOperation.m:191 Received authentication challenge
2013-06-26 14:03:08.016 Tests.com[1666:162f] T restkit.network:RKHTTPRequestOperation.m:156 POST 'http://api.test.com/account/Login' (200):
response.headers={
"Content-Length" = 1172;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 26 Jun 2013 04:03:04 GMT";
Server = "Microsoft-IIS/7.5";
Vary = "Accept-Encoding";
"X-Powered-By" = "ASP.NET";
}
response.body={"CustomerReference":"50713881","FirstName":"Mike","TotalBalance":22.25,"ErrorInfo":null,"Success":true}
2013-06-26 14:03:08.021 Tests.com[1666:3b13] W restkit.object_mapping:RKMapperOperation.m:76 Adding mapping error: Cannot map a collection of objects onto a non-mutable collection. Unexpected destination object type 'LoginRequest'
(lldb)
Why is it going on about LoginRequest?? I'm so lost!!
Any help would be very much appreciated.