Hello!
I am new to objective-c and also the use of RestKit. I'm using version 0.10.0 of this framework and I'm having trouble sending an object via post.
First I would like to explain a little bit of structure that I created in order to contextualize:
I created a class for which I will perform all posts by all necessary objects. This class also make the posts, also maps the objects.
//-------------------------------------------------------------------------------------------------------------------------------------//
TransportController.h :
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import <RestKit/RKJSONParserJSONKit.h>
@interface TransportController : NSObject<RKRequestDelegate, RKObjectLoaderDelegate>{
NSMutableArray * dataList;
NSString * entity;
NSPredicate * predicate;
NSObject * object;
NSManagedObjectContext * managedObjectContext;
Boolean * save;
}
@property (strong, nonatomic) NSMutableArray * dataList;
@property (strong, nonatomic) NSString * entity;
@property (strong, nonatomic) NSPredicate * predicate;
@property (strong, nonatomic) NSObject * object;
@property (strong, nonatomic) NSManagedObjectContext * managedObjectContext;
@property (nonatomic) Boolean * save;
+(void)setupAuthenticateMapping;
+(void)setupCreateAccountMapping;
+(void)setupLoacationMapping;
-(void)authenticateUser:(NSString *)email :(NSString *)pass;
-(id)initWithEntity:(NSString *)entity withPredicate:(NSPredicate *)predicate forObject:(NSObject *)object withManagedContext:(NSManagedObjectContext *)managedObjectContext saveId:(Boolean *)save;
@end
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------//
TransportController.m
#import "TransportController.h"
#import "AuthenticateOut.h"
#import "LocationTransp.h"
#import "CreateAccountOut.h"
#import "CoreDataHelper.h"
#import "User.h"
@implementation TransportController
@synthesize dataList = _dataList;
@synthesize entity = _entity;
@synthesize predicate = _predicate;
@synthesize object = _object;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize save = _save;
/* Initiate the mapping for authentication transport*/
+(void)setupAuthenticateMapping {
RKObjectMapping * authenticateMapping = [RKObjectMapping mappingForClass:[AuthenticateOut class]];
[authenticateMapping mapKeyPath:@"em" toAttribute:@"em"];
[authenticateMapping mapKeyPath:@"pd" toAttribute:@"pd"];
[[RKObjectManager sharedManager].mappingProvider setMapping:authenticateMapping forKeyPath:@"authenticate"];
RKObjectMapping *authenticateSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[authenticateSerializationMapping mapKeyPath:@"em" toAttribute:@"em"];
[authenticateSerializationMapping mapKeyPath:@"pd" toAttribute:@"pd"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:authenticateSerializationMapping forClass:[AuthenticateOut class]];
[[RKObjectManager sharedManager].router routeClass:[AuthenticateOut class] toResourcePath:@"/authenticate" forMethod:RKRequestMethodPOST];
}
/* Initiate the mapping for create account transport*/
+(void)setupCreateAccountMapping {
RKObjectMapping* userLocationMapping = [RKObjectMapping mappingForClass:[LocationTransp class]];
[userLocationMapping mapKeyPath:@"ad" toAttribute:@"ad"];
[userLocationMapping mapKeyPath:@"cy" toAttribute:@"cy"];
[userLocationMapping mapKeyPath:@"st" toAttribute:@"st"];
[userLocationMapping mapKeyPath:@"zp" toAttribute:@"zp"];
[userLocationMapping mapKeyPath:@"co" toAttribute:@"co"];
[userLocationMapping mapKeyPath:@"lt" toAttribute:@"lt"];
[userLocationMapping mapKeyPath:@"lg" toAttribute:@"lg"];
RKObjectMapping * userMapping = [RKObjectMapping mappingForClass:[CreateAccountOut class]];
[userMapping mapKeyPath:@"em" toAttribute:@"em"];
[userMapping mapKeyPath:@"na" toAttribute:@"na"];
[userMapping mapKeyPath:@"pd" toAttribute:@"pd"];
[userMapping mapKeyPath:@"lo" toRelationship:@"lo" withMapping:userLocationMapping ];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"createAccount"];
RKObjectMapping* userSerializationMapping = [userMapping inverseMapping];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:userSerializationMapping forClass:[CreateAccountOut class]];
[[RKObjectManager sharedManager].router routeClass:[CreateAccountOut class] toResourcePath:@"/createAccount" forMethod:RKRequestMethodPOST];
}
/* Initiate the mapping for location transport*/
+(void)setupLoacationMapping {
RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[LocationTransp class]];
[locationMapping mapKeyPath:@"id" toAttribute:@"id"];
[locationMapping mapKeyPathsToAttributes:@"ad",@"cy",@"st",@"zp",@"co",@"la",@"lo",nil];
[[RKObjectManager sharedManager].mappingProvider setMapping:locationMapping forKeyPath:@"json"];
RKObjectMapping *userSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[userSerializationMapping mapKeyPath:@"id" toAttribute:@"id"];
[userSerializationMapping mapKeyPath:@"ad" toAttribute:@"ad"];
[userSerializationMapping mapKeyPath:@"cy" toAttribute:@"cy"];
[userSerializationMapping mapKeyPath:@"st" toAttribute:@"st"];
[userSerializationMapping mapKeyPath:@"zp" toAttribute:@"zp"];
[userSerializationMapping mapKeyPath:@"co" toAttribute:@"co"];
[userSerializationMapping mapKeyPath:@"la" toAttribute:@"la"];
[userSerializationMapping mapKeyPath:@"lo" toAttribute:@"lo"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:userSerializationMapping forClass:[AuthenticateOut class]];
[[RKObjectManager sharedManager].router routeClass:[LocationTransp class] toResourcePath:@"/json" forMethod:RKRequestMethodPOST];
}
-(void)authenticateUser:(NSString *)email :(NSString *)pass{
[[RKObjectManager sharedManager].client setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
AuthenticateOut * authenticateOut = [AuthenticateOut new];
[authenticateOut setEm:email];
[authenticateOut setPd:pass];
RKObjectMapping *authenticateSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[authenticateSerializationMapping mapKeyPath:@"co" toAttribute:@"co"];
[authenticateSerializationMapping mapKeyPath:@"mg" toAttribute:@"mg"];
[authenticateSerializationMapping mapKeyPath:@"id" toAttribute:@"id"];
[authenticateSerializationMapping mapKeyPath:@"na" toAttribute:@"na"];
[[RKObjectManager sharedManager] postObject:authenticateOut mapResponseWith:authenticateSerializationMapping delegate:self];
}
-(id)initWithEntity:(NSString *)entity withPredicate:(NSPredicate *)predicate forObject:(NSObject *)object withManagedContext:(NSManagedObjectContext *)managedObjectContext saveId:(Boolean *)save{
if (self = [super init])
{
[self setEntity:entity];
[self setPredicate:predicate];
[self setObject:object];
[self setManagedObjectContext:managedObjectContext];
[self setSave:save];
}
return self;
}
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
RKLogInfo(@"Request Headers: %@", [response allHeaderFields]);
RKLogInfo(@"Cookies: %@", [response cookies]);
if ([response isSuccessful]) {
NSString *bodyResponse = [response bodyAsString];
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData: [bodyResponse dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &error];
if (_save) {
NSArray *responseObjectId = [json valueForKey:@"id"];
if ([CoreDataHelper countForEntity:_entity withPredicate:_predicate andContext:_managedObjectContext] > 0){
_dataList = [CoreDataHelper searchObjectsForEntity:_entity withPredicate:_predicate andSortKey:nil andSortAscending:YES andContext:_managedObjectContext];
if([object isKindOfClass:[User class]]){
User *user = (User *)[_dataList objectAtIndex:0];
user.taxId = (NSNumber *)responseObjectId;
}
NSError *error;
if (![_managedObjectContext save:&error])
{
NSLog(@"Failed to save user: %@", [error domain]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authenticating"
message:@"Was an error while authenticating user."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Ocorreu um erro ao tentar salvar o objeto", error );
}
}
}
}else if ([response isError]) {
RKLogInfo(@"Ouch! We have an HTTP error. Status Code description: %@", [response localizedStatusCodeString]);
}
}
@end
//---------------------------------------------------------------------------------------------------------------------------------//
In my ViewController I instantiate my transport class and call the post this way:
TransportController * transportController = [[TransportController alloc] initWithEntity:@"User" withPredicate:predicate forObject:user withManagedContext:managedObjectContext saveId:YES];
[transportController authenticateUser:[self.txtEmail text] :[self.txtPass text]];
The request is mounted but is not sent. In the console I can see that the request is ready to be sent, but what happens is that it returns to the class where I call the object of transport (ViewController) and gets into loop:
http://..../taxMileageServer/rest/service/>'. HTTP Headers: {
Accept = "application/json";
Connection = "Keep-Alive";
"Content-Length" = 38;
"Content-Type" = "application/json";
}. HTTP Body: {"pd":"senha","em":"ema...@email.com"}.
When I take the code that sends my transportation class (TrasnportController) and put the code right in the ViewController, the post is sent without problems. The code I put in the ViewController is the same that is in my transportation class:
ViewController:
[[RKObjectManager sharedManager].client setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
AuthenticateOut * authenticateOut = [AuthenticateOut new];
[authenticateOut setEm:[self.txtEmail text]];
[authenticateOut setPd:[self.txtPass text]];
RKObjectMapping *authenticateSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[authenticateSerializationMapping mapKeyPath:@"co" toAttribute:@"co"];
[authenticateSerializationMapping mapKeyPath:@"mg" toAttribute:@"mg"];
[authenticateSerializationMapping mapKeyPath:@"id" toAttribute:@"id"];
[authenticateSerializationMapping mapKeyPath:@"na" toAttribute:@"na"];
[[RKObjectManager sharedManager] postObject:authenticateOut mapResponseWith:authenticateSerializationMapping delegate:self];
TransportController:
[[RKObjectManager sharedManager].client setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
AuthenticateOut * authenticateOut = [AuthenticateOut new];
[authenticateOut setEm:email];
[authenticateOut setPd:pass];
RKObjectMapping *authenticateSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[authenticateSerializationMapping mapKeyPath:@"co" toAttribute:@"co"];
[authenticateSerializationMapping mapKeyPath:@"mg" toAttribute:@"mg"];
[authenticateSerializationMapping mapKeyPath:@"id" toAttribute:@"id"];
[authenticateSerializationMapping mapKeyPath:@"na" toAttribute:@"na"];
[[RKObjectManager sharedManager] postObject:authenticateOut mapResponseWith:authenticateSerializationMapping delegate:self];
Could anyone tell me why this happens? What I'm doing rong??
Thank's
Angelo