I've been trying to map the following object from the JSON response and from everything I see in the console output, there isn't any reason why the mapping isn't successful - I appreciate if anyone could have a check and see:
#interface RKElectionsModel : NSObject
#property (nonatomic, assign) bool isActive;
#property (nonatomic, strong) NSNumber *electionID;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *summary;
#property (nonatomic, strong) NSNumber *availableSeats;
#property (nonatomic, strong) NSNumber *candidatesCount;
#property (nonatomic, strong) NSNumber *withdrawnCount;
#property (nonatomic, strong) NSSet *candidates;
#end
/**
* Election Detail Mapping: Getting all election details, we have some extra information from
* the API call
*
*/
RKObjectMapping *electionDetailsMapping = [RKObjectMapping mappingForClass:[RKElectionsModel class]];
// Map JSON -> entities
[electionDetailsMapping addAttributeMappingsFromDictionary:#{
#"id": #"electionID",
#"title": #"title",
#"summary": #"summary",
#"active": #"isActive",
#"availableSeats": #"availableSeats",
#"candidatesCount": #"candidatesCount",
#"withdrawnCount": #"withdrawnCount"
}];
// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:electionDetailsMapping pathPattern:#"/api/elections/:electionID/all" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Console output on pastebin and any examples of the JSON response you can visit here
Appreciate any help,
Lewis
Seems like your trying to map a NSString to NSNumber.
In your NSObject try to add the following validation and transform the string you receive from the server to a NSNumber when the mapping starts.
- (BOOL)validateAvailableSeats:(id *)ioValue error:(NSError **)outError {
// Force the value to be a NSNumber
*ioValue = [NSNumber numberWithDouble:[(NSString*)value doubleValue] ]
return YES;
}
In the RestKit Wiki you'll find more transformations for your values.
Related
I am attempting to use RestKit to parse some JSON. The app is for tracking competitions, and has a concept of venues, games, and players. A game has a winner and loser (both of class Player) and a venue. The JSON returned by the web service looks like this:
[{"id":1,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME3","id":3,"picture_url":"NJ.png"},"venue":{"name":"Venue 1","id":2}},
{"id":2,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME1","id":1,"picture_url":"NC.png"},"venue":{"name":"Venue 1","id":2}},
{"id":3,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME3","id":3,"picture_url":"NJ.png"},"venue":{"name":"Venue 1","id":2}},
{"id":4,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME1","id":1,"picture_url":"NC.png"},"venue":{"name":"Venue 1","id":2}}]
My object header files look like this:
# PTGame.h
#interface PTGame : NSObject
#property (nonatomic, assign) NSNumber *uid;
#property (nonatomic, assign) PTPlayer *winner;
#property (nonatomic, assign) PTPlayer *loser;
#property (nonatomic, assign) PTVenue *venue;
#end
# PTVenue.h
#interface PTVenue : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, assign) NSNumber *uid;
-(CLLocation *)location;
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;
#end
# PTPlayer.h
#interface PTPlayer : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *pictureUrl;
#property (nonatomic, assign) NSNumber *uid;
#end
Finally, my mapping setup looks like:
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[PTVenue class]];
[venueMapping addAttributeMappingsFromDictionary:#{#"name" : #"name",
#"id" : #"uid"}];
RKObjectMapping *playerMapping = [RKObjectMapping mappingForClass:[PTPlayer class]];
[playerMapping addAttributeMappingsFromDictionary:#{#"name" : #"name",
#"id" : #"uid",
#"picture_url": #"pictureUrl"}];
// Add Object Mapping for PTGame
RKObjectMapping *gameResponseMapping = [RKObjectMapping mappingForClass:[PTGame class]];
// Setup Game Object relationships
[gameResponseMapping addAttributeMappingsFromDictionary:#{#"id": #"uid"}];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:#"winner" mapping:playerMapping];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:#"loser" mapping:playerMapping];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:#"venue" mapping:venueMapping];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *gameListResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:gameResponseMapping
method:RKRequestMethodGET
pathPattern:#"/games.json"
keyPath:nil
statusCodes:statusCodes];
[objectManager addResponseDescriptor:gameListResponseDescriptor];
This all looks right to me. No errors are thrown when the result is returned, but later I get an exception when attempting to access a property of the game. A peek into the objects via XCode debugger shows this:
Something seems to be broken - why do those strings show as "winner" and "venue". Why is the uid null? The objects seem different every time I make the request, leading me to believe the memory is not be handled properly.
Any ideas?
Wain's comment led me down the path of looking outside of the mapping code. I looked in the RestKit Object Mapping docs and noticed all of the attributes were using the nonatomic and copy properties, rather than assign. Additionally, the object references (such as PTPlayer *winner were simply nonatomic. Changing my code to reflect this fixed the issue.
The resulting header files look like:
# PTPlayer.h
#interface PTPlayer : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *pictureUrl;
#property (nonatomic, copy) NSNumber *uid;
#end
# PTVenue.h
#interface PTVenue : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSNumber *uid;
#property (nonatomic, assign) CGFloat longitude;
#property (nonatomic, assign) CGFloat latitude;
-(CLLocation *)location;
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;
#end
# PTGame.h
#interface PTGame : NSObject
#property (nonatomic, copy) NSNumber *uid;
#property (nonatomic) PTPlayer *winner;
#property (nonatomic) PTPlayer *loser;
#property (nonatomic) PTVenue *venue;
#end
I have complex JSON handling large amount of data, I need to optimise network traffic by sending only required attributes of mapped object to server.
For simplicity lets say I have following User class :
#property (nonatomic, retain) NSString *email;
#property (nonatomic, retain) NSString *fname;
#property (nonatomic, retain) NSString *password;
#property (nonatomic, retain) NSString *profilePic;
#property (nonatomic, retain) NSString *sname;
#property (nonatomic, retain) NSString *status;
#property (nonatomic, retain) NSString *token;
#property (nonatomic, retain) NSString *username;
#property (nonatomic, retain) NSNumber *isLoggedIn;
#property (nonatomic, retain) NSDate *dateCreated;
and my attributes mapping dictionary is following :
[dic addEntriesFromDictionary:#{
#"fname": #"fname",
#"sname": #"sname",
#"profilePic": #"profilePic",
#"email": #"email",
#"username": #"username",
#"password": #"password",
#"status": #"status",
#"token": #"token",
#"isLoggedIn": #"isLoggedIn",
#"dateCreated": #"dateCreated"
}];
For Signin call I needs to post just username & password as following JSON :
{
"user": {
"password": "password",
"username": "demouser"
}
}
While for Signup call I needs to POST entire User object so I cant downsize mapping dictionary. I needs to apply same procedure to lot more complex JSON.
How can I send required attributes of an object in POST call on conditional basis in an optimal fashion?
Thanks.
You are free to create multiple mappings for the same class / entity type - there is no restriction. Each mapping is associated with other mappings / request descriptor / response descriptor and this is where you need to concentrate on identification and uniqueness.
It may be simplest for you to have one request mapping which covers all of the attributes, and whose class is NSDictionary. Then, to use this mapping for a request you use KVC (dictionaryWithValuesForKeys:) to extract only the keys of interest from your true source object into a dictionary that you can then supply to the object manager for mapping and transmission.
I've got the following JSON coming from my webservice:
"GasPrices":{
"Ai92":{
"Price":30.1000,
"LastDate":"\/Date(1385337600000)\/",
"Votes":0
},
"Ai95":{
"Price":33.2000,
"LastDate":"\/Date(1385337600000)\/",
"Votes":0
}
I want to map it to NSDictionary, whose keys would be NSStrings and values would be of my custom class, say, PriceInfo.
What I got now with default setup is NSDictionary whose values are also NSDictionaries.
How can I achieve the desired mapping?
UPD. Here's my full setup for now.
#interface FillingStation : NSObject
#property (nonatomic, strong) NSNumber *uid;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSDictionary *fuelPrices;
#end
#interface PriceInfo : NSObject
#property (nonatomic, strong) NSNumber *price;
#property (nonatomic, strong) NSDate *lastDate;
#property (nonatomic, strong) NSNumber *votes;
#end
Configuring the mapping:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[FillingStation class]];
[mapping addAttributeMappingsFromDictionary:#{
#"Id" : #"uid",
#"Title" : #"title",
#"GasPrices" : #"fuelPrices"
}];
This results in fuelPrices is NSDictionary with the structure:
NSString -> NSDictionary,
NSString -> NSDictionary,
...
And I want it to be:
NSString -> PriceInfo,
NSString -> PriceInfo,
...
And I don't want an intermediate dictionary in FillingStation which I can then manually map.
Okay, seems like I found the answer. Not exactly what I wanted, yet acceptable for me: https://github.com/RestKit/RestKit/wiki/Object-Mapping#handling-dynamic-nesting-attributes
I had to add another property to PriceInfo class and change fuelPrices from NSDictionary to NSSet (NSArray would also work, but I don't need ordering), so it became:
#interface FillingStation : NSObject
#property (nonatomic, strong) NSNumber *uid;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSSet *fuelPrices;
#end
#interface PriceInfo : NSObject
#property (nonatomic, strong) NSString *fuelType;
#property (nonatomic, strong) NSNumber *price;
#property (nonatomic, strong) NSDate *updateDate;
#property (nonatomic, assign) NSUInteger votes;
#end
and my mapping now looks like this:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[FillingStation class]];
[mapping addAttributeMappingsFromDictionary:#{
#"Id" : #"uid",
#"Title" : #"title"
}];
RKObjectMapping *priceInfoMapping = [RKObjectMapping mappingForClass:[PriceInfo class]];
[priceInfoMapping setForceCollectionMapping:YES];
[priceInfoMapping addAttributeMappingFromKeyOfRepresentationToAttribute:#"fuelType"];
[priceInfoMapping addAttributeMappingsFromDictionary:#{
#"(fuelType).Price": #"price",
#"(fuelType).LastDate": #"updateDate",
#"(fuelType).Votes": #"votes"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"GasPrices"
toKeyPath:#"fuelPrices"
withMapping:priceInfoMapping]];
Jastor May be what you're looking for. Nested objects directly mapped to known classes.
I have been struggling with this issue for some time now and I cant figure out why the JSON serialization is not working. What seems to be a simple task it taking way too long.
I have my model classes listed below. User object has NSArray of Education and Employment objects.
I am using RestKit and all of the relationships have been made and I can see that the dictionary of the parameters is being made the data is correct. In order to debug the issue further I am making JSON using the following code.
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:self.user requestDescriptor:delegate.postUserRequestDescriptor error:&error];
// Serialize the object to JSON
BOOL isTurnableToJSON = [NSJSONSerialization
isValidJSONObject: parameters];
if(isTurnableToJSON){
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
}
isTurnableToJSON is always "NO". When I force creation of JSON I get the following error.
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Education)'
The same error is also thrown for employment if I remove the relationship of Education with user.
Here is my user, education, and location models.
#interface User : NSObject
#property (strong, nonatomic) NSString *first_name;
#property (strong, nonatomic) NSString *middle_name;
#property (strong, nonatomic) NSString *last_name;
#property (strong, nonatomic) NSString *profile_picture;
#property (strong, nonatomic) NSString *display_name;
#property (nonatomic, strong) NSArray *education;
#property (nonatomic, strong) NSArray *employment;
#property (nonatomic, strong) NSMutableArray *skills;
#property (strong, nonatomic) NSString *about_you;
#end
#interface Education : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *graduation_date;
#property (nonatomic, strong) NSString *major;
#property (nonatomic, strong) NSString *degree;
#property (nonatomic, strong) Location *location;
#property (nonatomic) NSNumber *current;
#end
#interface Location : NSObject
#property (nonatomic, strong) NSString *city;
#property (nonatomic, strong) NSString *state;
#property (nonatomic, strong) NSString *country;
#end
At this point I am at lose of any ideas and any help would be appreciated.
Update: RestKit Relationship mapping.
RKObjectMapping *locationMap = [RKObjectMapping mappingForClass:(Location.class)];
[locationMap addAttributeMappingsFromDictionary:#{#"city":#"city",#"state":#"state",#"country":#"country"}];
RKObjectMapping *educationMap = [RKObjectMapping mappingForClass:(Education.class)];
[educationMap addAttributeMappingsFromDictionary:# { #"name":#"name",#"graduation_date":#"graduation_date",#"major":#"major", #"degree":#"degree",#"current":#"current"}];
[educationMap addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"location" toKeyPath:#"location" withMapping:locationMap]];
RKObjectMapping *employmentMap =[RKObjectMapping mappingForClass:(Employment.class)];
[employmentMap addAttributeMappingsFromDictionary:#{#"company":#"company",#"occupation":#"occupation", #"start_date":#"start_date",#"end_date":#"end_date",#"current":#"current"}];
RKObjectMapping *userPutRequestMapping = [RKObjectMapping requestMapping];
[userPutRequestMapping addAttributeMappingsFromDictionary:putUserMap];
[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"education" toKeyPath:#"education" withMapping:[educationMap inverseMapping]]];
[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"employment" toKeyPath:#"employment" withMapping:[employmentMap inverseMapping]]];
USER PUT REQUEST
[objectManager addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:userPutRequestMapping
objectClass:[User class]
rootKeyPath:nil
method:RKRequestMethodPUT]];
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":"clientTest#Test.com","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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "VOUser.h"
#import "VOGateway.h"
#import "VOAuth.h"
#import "SOAuth.h"
#interface MAModule : NSObject <RKObjectLoaderDelegate>
-(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 <RKClient: 0x6d06e10>, 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 <RKClient: 0x6d06e10>, unsuspending queue <RKRequestQueue: 0x6d09c10 name=(null) suspended=YES requestCount=1 loadingCount=0/5>
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 <SOAuth: 0x9001540>, 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 '<NSMutableURLRequest http://mydomain.dev/ui/v1/auth>'. 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":"clientTest#Test.com","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.
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.