iOS – POST with RestKit : callback functions not called – (mapping data used)-Collection of common programming errors

Hi all it’s my first post in stack-overflow,

So First, thank you very much to everyone who shares!

My problems are :

  • I dont have callback return when I use RKObjectManager for POST Serialized object.
  • And I don’t know if my use of Mapping data is correct …

So I show you the result of my restful service (formated in JSON), my code and my restkit Logs

{"sessionId":"DA93D5ECD8E338AA27800794EEB9C20F","user":{"id":3,"ref":"2461498766","login":"clientTest","mail":"[email protected]","phone":"client127","subId":3,"creation":"29/03/2010 10:33:24","language":"en","firstConnection":"30/03/2010 16:42:07","lastConnection":"02/11/2011 09:36:43","connectionStep":"6"},"gateway":{"id":3,"serial":"testserial","status":"A","led":"START","ref":"DJJHGGGG00009","type":"FULL","ip":"192.168.44.168","connection":"","initDate":1290694795533,"activationDate":1290694795533,"manufacDate":1269851384000,"imei":"354482020013035","model":"TYLOP","firmware":"1.3.1","version":"","macWifi":"...","macEthernet":"...","updateDate":1310053274000,"muteMode":"","timezone":""}}

As you can see, we have a global Object with inside : User, Gateway Objects.

VOAuth.h, my global object used for data mapping :

#import "VOUser.h"
#import "VOGateway.h"

@interface VOAuth : NSObject
@property (nonatomic, retain) NSString * sessionId;
@property (nonatomic, retain) VOUser * user;
@property (nonatomic, retain) VOGateway * gateway;
@end

VOGateway.h,

#import 

@interface VOGateway : NSObject

@property (nonatomic, retain) NSString * identifier;
@property (nonatomic, retain) NSString * serial;
@property (nonatomic, retain) NSString * status;
@property (nonatomic, retain) NSString * led;
@property (nonatomic, retain) NSString * ref;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * ip;
@property (nonatomic, retain) NSString * connection;
@property (nonatomic, retain) NSString * initDate;
@property (nonatomic, retain) NSString * activationDate;
@property (nonatomic, retain) NSString * manufacDate;
@property (nonatomic, retain) NSString * imei;
@property (nonatomic, retain) NSString * model;
@property (nonatomic, retain) NSString * firmware;
@property (nonatomic, retain) NSString * version;
@property (nonatomic, retain) NSString * macWifi;
@property (nonatomic, retain) NSString * macEthernet;
@property (nonatomic, retain) NSString * updateDate;
@property (nonatomic, retain) NSString * muteMode;
@property (nonatomic, retain) NSString * timezone;

@end

VOUser.h,

#import 

@interface VOUser : NSObject

@property (nonatomic, retain) NSString * identifier;
@property (nonatomic, retain) NSString * ref;
@property (nonatomic, retain) NSString * login;
@property (nonatomic, retain) NSString * mail;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * subId;
@property (nonatomic, retain) NSString * creation;
@property (nonatomic, retain) NSString * language;
@property (nonatomic, retain) NSString * firstConnection;
@property (nonatomic, retain) NSString * lastConnection;
@property (nonatomic, retain) NSString * connectionStep;

@end

SOAuth.h (my serialized object used as parameters for my call),

#import 

@interface SOAuth : NSObject
@property (nonatomic, retain) NSString* login;
@property (nonatomic, retain) NSString* password;
@end

MAModule.h (My manger, i call my restfull service here),

#import 
#import 
#import "VOUser.h"
#import "VOGateway.h"
#import "VOAuth.h"
#import "SOAuth.h"
@interface MAModule : NSObject 
-(void)sendLogIn;
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;
@end

MAModule.m,

#import "MAModule.h"

@implementation MAModule

-(void)sendLogIn
{

RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

RKObjectManager* manager = [RKObjectManager    objectManagerWithBaseURL:@"http://mydomain.dev/ui/v1"];
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
[manager.router routeClass:[SOAuth class] toResourcePath:@"/auth" forMethod:RKRequestMethodPOST];

RKObjectMapping* authSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class] ];
[authSerializationMapping mapAttributes:@"login", @"password", nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:authSerializationMapping forClass:[SOAuth class] ];

RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[VOUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[userMapping mapKeyPath:@"ref" toAttribute:@"ref"];
[userMapping mapKeyPath:@"login" toAttribute:@"login"];
[userMapping mapKeyPath:@"mail" toAttribute:@"mail"];
[userMapping mapKeyPath:@"phone" toAttribute:@"phone"];
[userMapping mapKeyPath:@"subId" toAttribute:@"subId"];
[userMapping mapKeyPath:@"creation" toAttribute:@"creation"];
[userMapping mapKeyPath:@"language" toAttribute:@"language"];
[userMapping mapKeyPath:@"firstConnection" toAttribute:@"firstConnection"];
[userMapping mapKeyPath:@"lastConnection" toAttribute:@"lastConnection"];
[userMapping mapKeyPath:@"connectionStep" toAttribute:@"connectionStep"];


RKObjectMapping *gatewayMapping = [RKObjectMapping mappingForClass:[VOGateway class]];
[gatewayMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[gatewayMapping mapKeyPath:@"serial" toAttribute:@"serial"];
[gatewayMapping mapKeyPath:@"status" toAttribute:@"status"];
[gatewayMapping mapKeyPath:@"led" toAttribute:@"led"];
[gatewayMapping mapKeyPath:@"ref" toAttribute:@"ref"];
[gatewayMapping mapKeyPath:@"type" toAttribute:@"type"];
[gatewayMapping mapKeyPath:@"ip" toAttribute:@"ip"];
[gatewayMapping mapKeyPath:@"connection" toAttribute:@"connection"];
[gatewayMapping mapKeyPath:@"initDate" toAttribute:@"initDate"];
[gatewayMapping mapKeyPath:@"activationDate" toAttribute:@"activationDate"];
[gatewayMapping mapKeyPath:@"manufacDate" toAttribute:@"manufacDate"];
[gatewayMapping mapKeyPath:@"imei" toAttribute:@"imei"];
[gatewayMapping mapKeyPath:@"model" toAttribute:@"model"];
[gatewayMapping mapKeyPath:@"firmware" toAttribute:@"firmware"];
[gatewayMapping mapKeyPath:@"version" toAttribute:@"version"];
[gatewayMapping mapKeyPath:@"macWifi" toAttribute:@"macWifi"];
[gatewayMapping mapKeyPath:@"macEthernet" toAttribute:@"macEthernet"];
[gatewayMapping mapKeyPath:@"updateDate" toAttribute:@"updateDate"];
[gatewayMapping mapKeyPath:@"muteMode" toAttribute:@"muteMode"];
[gatewayMapping mapKeyPath:@"timezone" toAttribute:@"timezone"];

RKObjectMapping *authReturnMapping = [RKObjectMapping mappingForClass:[VOAuth class]];
[authReturnMapping mapKeyPath:@"sessionId" toAttribute:@"sessionId"];
[authReturnMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping];
[authReturnMapping mapKeyPath:@"gateway" toRelationship:@"gateway" withMapping:gatewayMapping];
[[RKObjectManager sharedManager].mappingProvider setMapping:authReturnMapping forKeyPath:@""];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[authReturnMapping inverseMapping] forClass:[VOAuth class]]; 

NSLog(@"LOGIN SEND --------------------------------");    
SOAuth *logObj = [[SOAuth alloc]init];
logObj.login = @"clientTest";
logObj.password = @"216a2e1269b5daaa35fd911964e5a86ce11f267d";

RKObjectMapping* authhMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[VOAuth class] ];
[[RKObjectManager sharedManager] postObject:logObj mapResponseWith:authhMapping delegate:nil];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
//RKLogInfo(@"Load collection of Articles: %@", objects);
NSLog(@"LOGIN OK --------------------------------");
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"LOGIN KO --------------------------------");
}

@end

I would like to highlight few points :

  • [[RKObjectManager sharedManager].mappingProvider setMapping:authReturnMapping forKeyPath:@””] : I have an empty key for my global object mapping, because I don’t have root key for it …

  • [[RKObjectManager sharedManager] postObject:logObj mapResponseWith:authhMapping delegate:nil]; My delegate is nil, else my application crash …

LOGS :

2011-11-02 11:04:50.824 RestKit Installation[3701:207] D restkit.network:RKClient.m:265 Reachability observer changed for client , suspending queue (null) until reachability to host '0.0.0.0' can be determined
2011-11-02 11:04:50.827 RestKit Installation[3701:207] LOGIN SEND --------------------------------
2011-11-02 11:04:50.833 RestKit Installation[3701:207] D restkit.network:RKClient.m:378 Reachability to host '0.0.0.0' determined for client , unsuspending queue 
2011-11-02 11:04:50.836 RestKit Installation[3701:207] D restkit.network:RKRequest.m:334 Sending asynchronous POST request to URL http://mydomain.dev/ui/v1/auth.
2011-11-02 11:04:50.836 RestKit Installation[3701:207] D restkit.network:RKObjectLoader.m:302 POST or PUT request for source object , serializing to MIME Type application/json for transport...
2011-11-02 11:04:50.838 RestKit Installation[3701:207] T restkit.network:RKRequest.m:282 Prepared POST URLRequest ''. HTTP Headers: {
Accept = "application/json";
"Content-Length" = 75;
"Content-Type" = "application/json";
}. HTTP Body:       {"login":"clientTest","password":"216a2e1269b5daaa35fd911964e5a86ce11f267d"}.
2011-11-02 11:04:51.432 RestKit Installation[3701:207] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 200
2011-11-02 11:04:51.433 RestKit Installation[3701:207] D restkit.network:RKResponse.m:197 Headers: {
Connection = close;
"Content-Length" = 763;
"Content-Type" = "application/json";
Date = "Wed, 02 Nov 2011 10:04:57 GMT";
}
2011-11-02 11:04:51.434 RestKit Installation[3701:207] T restkit.network:RKResponse.m:202 Read response body: {"sessionId":"DA93D5ECD8E338AA27800794EEB9C20F","user":{"id":3,"ref":"2461498766","login":"clientTest","mail":"[email protected]","phone":"client127","subId":3,"creation":"29/03/2010 10:33:24","language":"en","firstConnection":"30/03/2010 16:42:07","lastConnection":"02/11/2011 09:36:43","connectionStep":"6"},"gateway":{"id":3,"serial":"testserial","status":"A","led":"START","ref":"DJJHGGGG00009","type":"FULL","ip":"192.168.44.168","connection":"","initDate":1290694795533,"activationDate":1290694795533,"manufacDate":1269851384000,"imei":"354482020013035","model":"TYLOP","firmware":"1.3.1","version":"","macWifi":"...","macEthernet":"...","updateDate":1310053274000,"muteMode":"","timezone":""}}
2011-11-02 11:04:51.438 RestKit Installation[3701:6003] D restkit.network:RKObjectLoader.m:210 Found directly configured object mapping, creating temporary mapping provider for keyPath '%@'

Finally,

My callback functions are not called :

  • (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

  • (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error

Hoping someone can help me,

TY.

  1. Your methods are not called, because you set nil as your delegate. As per our later discussion, if self is passed as the delegate, the subsequent crash is caused by MAModule being released too early. Use either property or singleton pattern to retain the MAModule object at least until your requests are completed.