Implementing object mapping using RestKit in Objective-C - objective-c

I am having trouble mapping a JSON response to objects using RestKit and Objective-C.
I have already set up my RKObjectManager and mappings in my AppDelegate as suggested in my previous post by mja.
I call my backend in my controller as per the example below.
There are two issues I'm having trouble with:
[[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self]; } <-- This results in a "self" is incompatible with type id. What do I need to send to this?
How do I cast the result in didLoadObject to the Translation object I've defined (translationText)
Any help would be much appreciated.
#synthesize inputtext = _text;
#synthesize translation = _translation;
#synthesize translatedText = _translatedText;
- (Translation *)translatedText {
if (!_translatedText) _translatedText = [[Translation alloc] init];
return _translatedText; }
- (IBAction)translatePressed {
//create TranslationRequest
TranslationRequest *request = [[TranslationRequest alloc] init];
[request setSourceId:#"1"];
[request setRegionTag:#"Hello"];
[request setInputString:self.inputtext.text];
//fetch the desired mapping to map response with
RKObjectMapping * responseMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Translation class]];
[[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self]; }
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
self.translation.text = [object translatedText].translation;
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(#"Hit error: %#", error);
}

to rectify the first issue, declare your controler in the .h file as follows:
#import "RestKit/RestKit.h"
...
#interface MyController : UIViewController<RKObjectLoaderDelegate>
You cast it just like this:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
Translation *myTranslation = (Translation*)object;
...
}
or you can avoid the cast by calling appropriate selector
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
self.translation.text = [[object translatedText] translation];
}
you may update your question with the definition of #properties in your Translation object in order to be sure this answer is correct.

Related

How to wrap restkit calls into a service class, and integrate into a controller?

I'm just getting into objective-c, and need some help with this service class I want to write.
I want to create a APIService class, that uses restkit and returns the responses.
It looks like restkit is based on blocks, so when the http call returns, parses the json and returns the resulting collection, I have to somehow make my APIService methods return the response.
I'm looking for help on the skeleton structure of this service, as I am new to objective-c and restkit (which uses blocks).
I'm looking at this example which I want to setup in my own APIService class:
https://github.com/RestKit/RestKit/blob/development/Examples/RKTwitter/Classes/RKTwitterViewController.m#L18
- (void)loadTimeline
{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager getObjectsAtPath:#"/status/user_timeline/RestKit"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray* statuses = [mappingResult array];
NSLog(#"Loaded statuses: %#", statuses);
_statuses = statuses;
if(self.isViewLoaded)
[_tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
NSLog(#"Hit error: %#", error);
}];
}
Can someone help me flesh out this skeleton structure, with a method call like the timeline about, here is what I have now:
#interface MyApiService : NSObject
{
#property (nonatomic, strong, readwrite) RKObjectManager *rkObjectManager;
- (id)initWithRKObjectManager:(RKObjectManager *)rkObjectManager;
- (NSArray) loadTimeline:
}
#implementation MyApiService
{
- (id)initWithRKObjectManager:(RKObjectManager *)rkObjectManager
{
self = [super init];
if(self) {
self.rkObjectManager = rkObjectManager;
// ...
}
}
// how to define method for loadTimelines when the call returns using a block?
}
Then I'll use it like this:
// Initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
MyAPiService *service = [[MyApiService alloc] initWithRKObjectManager: objectManager];
NSArray *statuses = [service loadTimeLine];
But not sure if this is how I will call loadTimeLine since restkit again uses blocks??
RestKit also uses delegates. This is how i do:
Import #import <RestKit/RestKit.h> in your .h file.
Implement the delegate RKObjectLoaderDelegate
Load objects from a service like this.
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager loadObjectsAtResourcePath:#"//status/user_timeline/RestKit" delegate:self];
Implement the delegate methods:
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
// Do whatever with objects here...
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
NSLog(#"Error POSTing object : %#", error);
}
There are many useful delegate methods for RKObjectLoaderDelegate.
You can follow a nice RestKit tutorial here.

using connectiondidFinishLoading in an instance and return data to another view

i'm quite a beginner at cocoa an objective-c - so please forgive me a possibly trivial question.
I'm currently working on an XCODE-Project which is fetching some data via NSJSONSerialization and store it for further work.
At this step I'm going to encapsulate the progress of fetching the data into an class which has some setter for the needed parameters (url to fetch from and the layer which should be parsed into an array). In order to use this procedure im creating the method inside this class which creates a connection and a request and returns the array which should contain the data. After some tests I tried to create an instance of this class and called the method which starts to fetch the data.
My problem is, that after calling the method data_array from my new instance "block_stats"
and store the data in an array of the same type - the array is empty
table_data = [block_stats data_array];
The reason of this behavior is that the usage of the methods in (didReceiveResponse,didReceiveData,connectionDidFinishLoading) are working asynchron and the return of the data_array was done before the download was finished.
the method inside the class which contains the downloading part:
- (NSMutableArray *)data_array
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(data_array)
{
[data_array removeAllObjects];
} else {
data_array = [[NSMutableArray alloc] init];
}
NSURLRequest *request = [NSURLRequest requestWithURL:data_url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webdata = [[NSMutableData alloc]init];
}
return data_array;
}
the IBAction in another view which creates the instance and calls the method to fetch the data
- (IBAction)refresh:(UIBarButtonItem *)sender {
KDJSONparser *block_stats = [[KDJSONparser alloc]init];
[block_stats setURL:[NSURL URLWithString:#"*JSONDATA_URL*"]];
[block_stats setLayer:#"blocks"];
table_data = [block_stats data_array];
}
I would be very glad if anyone could give some advice. It would be very nice if it would be as easy as possible to understand for me. Thanks in advance!
The solution to your problem lies in delegation (As the word suggests you will be appointing some one else to take action when a situation presents itself.)
You have already used this in the following piece of your code.
NSURLRequest *request = [NSURLRequest requestWithURL:data_url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
Here when you have set self as the delegate for your NSURLConnection you are telling the compiler to send you any appropriate messages related to the connection. These messages include didReceiveResponse,didReceiveData,connectionDidFinishLoading.
So let's implement these methods in your class and they will look something like this.
KDJSONParser.m
- (NSMutableArray *)fetchData
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLRequest *request = [NSURLRequest requestWithURL:data_url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webdata = [[NSMutableData alloc]init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[webdata setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[webdata appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
//Parse the webdata here. Your array must be filled now.
[self parseTheJSONData];
}
-(void)parseTheJSONData
{
//Do your parsing here and fill it into data_array
[self.delegate parsedArray:data_array];
}
And in your other class add this line in you refresh method
block_stats.delegate = self;
and implement
-(void)parsedArray:(NSMutableArray *)data_array;

Passing delegate through another object with ARC

I've got 2 classes, MPRequest and MPModel.
The MPModel class has a method to lookup something from the core data store, and if not found, creates an MPRequest to retrieve it via a standard HTTP request (The method in MPModel is static and not and instance method).
What I want is to be able to get a progress of the current HTTP request. I know how to do this, but I'm getting a little stuck on how to inform the view controller. I tried creating a protocol, defining a delegate property in the MPRequest class, altering the method in MPModel to accept this delegate, and in turn passing it to the MPRequest when it is created.
This is fine, however ARC is then releasing this delegate whilst the request is running and thus doesn't do what I want. I'm trying to avoid making my delegate object a strong reference in case it throws up any reference cycles but I don't know any other way of doing this.
To start the request, from my view controller I'm running
[MPModel findAllWithBlock:^(NSFetchedResultsController *controller, NSError *error) {
....
} sortedBy:#"name" ascending:YES delegate:self]
Inside the findAllWithBlock method, I have
MPRequest *objRequest = [MPRequest requestWithURL:url];
objRequest.delegate = delegate;
[objRequest setRequestMethod:#"GET"];
[MPUser signRequest:objRequest];
[objRequest submit:^(MPResponse *resp, NSError *err) {
...
}
And in the MPRequest class I have the following property defined :
#property (nonatomic, weak) NSObject<MPRequestDelegate> *delegate;
Any ideas or suggestions?
As requested, here is some more code on how things are being called :
In the view controller :
[MPPlace findAllWithBlock:^(NSFetchedResultsController *controller, NSError *error) {
_placesController = controller;
[_listView reloadData];
[self addAnnotationsToMap];
[_loadingView stopAnimating];
if (_placesController.fetchedObjects.count > 0) {
// We've got our places, but if they're local copies
// only, new ones may have been added so just update
// our copy
MPSyncEngine *engine = [[MPSyncEngine alloc] initWithClass:[MPPlace class]];
engine.delegate = self;
[engine isReadyToSync:YES];
[[MPSyncManager sharedSyncManager] registerSyncEngine:engine];
[[MPSyncManager sharedSyncManager] sync];
}
} sortedBy:#"name" ascending:YES delegate:self];
Here, self is never going to be released for obvious reasons, so I don't see how this is the problem.
Above, MPPlace is a subclass of MPModel, but the implementation of the findAllWithBlock:sortedBy:ascending:delegate: is entirely in MPModel
The method within MPModel looks like this
NSManagedObjectContext *context = [[MPCoreDataManager sharedInstance] managedObjectContext];
[context performBlockAndWait:^{
__block NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([self class])];
[request setSortDescriptors:#[[[NSSortDescriptor alloc] initWithKey:key ascending:asc]]];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
[controller performFetch:&error];
if (!controller.fetchedObjects || controller.fetchedObjects.count == 0) {
// Nothing found or an error, query the server instead
NSString *url = [NSString stringWithFormat:#"%#%#", kMP_BASE_API_URL, [self baseURL]];
MPRequest *objRequest = [MPRequest requestWithURL:url];
objRequest.delegate = delegate;
[objRequest setRequestMethod:#"GET"];
[MPUser signRequest:objRequest];
[objRequest submit:^(MPResponse *resp, NSError *err) {
if (err) {
block(nil, err);
} else {
NSArray *objects = [self createListWithResponse:resp];
objects = [MPModel saveAllLocally:objects forEntityName:NSStringFromClass([self class])];
[controller performFetch:&error];
block(controller, nil);
}
}];
} else {
// Great, we found something :)
block (controller, nil);
}
}];
The delegate is simply being passed on to the MPRequest object being created. My initial concern was that the MPRequest object being created was being released by ARC (which I guess it probably is) but it didn't fix anything when I changed it. I can't make it an iVar as the method is static.
The submit method of the request looks like this :
_completionBlock = block;
_responseData = [[NSMutableData alloc] init];
[self prepareRequest];
[self prepareRequestHeaders];
_connection = [[NSURLConnection alloc] initWithRequest:_urlRequest
delegate:self];
And when the app starts downloading data, it calls :
[_responseData appendData:data];
[_delegate requestDidReceive:(float)data.length ofTotal:_contentLength];
Where _contentLength is simply a long storing the expected size of the response.
Got it working. It was partly an issue with threading, where the core data thread was ending before my request, me looking at the output from a different request entirely, and the way ARC handles memory in blocks.
Thanks for the help guys

EXC BAD Access RestKit

In ClassA.h
#interface ClassA : NSObject<RKObjectLoaderDelegate,RKRequestDelegate>
#property(nonatomic,strong)NSMutableDictionary *inputDict;
ClassA.m
//After Implementation
#synthesize inputDict;
-(void)sendRequestWithInputDict:(NSMutableDictionary*)inputDictVal
{
RKURL *baseURL = [RKURL URLWithBaseURLString:baseUrl];
RKObjectManager * manager = [RKObjectManager objectManagerWithBaseURL:baseURL];
[manager setClient:[RKClient sharedClient]];
manager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
RKObjectLoader *objectLoader = [manager loaderWithResourcePath:#"/getLocation"];
objectLoader.serializationMIMEType = RKMIMETypeJSON;
objectLoader.method = RKRequestMethodPOST;
objectLoader.params = inputDictVal;
objectLoader.delegate = self;
[objectLoader send];
}
-(void)getLocation
{
inputDict = [[NSMutableDictionary alloc]init];
[self sendRequest:inputDict];
}
baseUrl is declared in constant file which i am importing here.
I am trying to call a sendRequest Function from another class. But i get a EX_BAD_ACCESS in requestWillPrepareForSend(RKRequest.m).
i think some object is released automatically. i don't know which one is...
Check out the instance variables baseUrl and inputDict. Always use properties instead of instance variables and you will never face such problems.
There are many things that are wrong with your code. The most obvious is not retaining the object manager (unless this becomes the sharedManager) another is trying to load an object but using POST. Although, judging by the errors you report, I think your ClassA instance is being dealloced, and because it is set as a delegate you are getting EXC_BAD_ACCESS. I suggest you move to using the block based methods and not the delegate callbacks.
By using Blocks i can able to send request to the server and getting response from it. i found the nice tutorial here http://kalapun.com/blog/2012/05/17/how-i-restkit/
-(void)sendRequest:(NSMutableDictionary*)inputDict withResourcePath:(NSString*)resourcePath
{
RKURL *baseURL = [RKURL URLWithBaseURLString:baseUrl];
RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:baseURL];
[manager setClient:[RKClient sharedClient]];
[manager loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *objectLoader){
objectLoader.method = RKRequestMethodPOST;
objectLoader.params = inputDict;
objectLoader.onDidFailWithError = ^(NSError *error){
NSLog(#"Error: %#", [error localizedDescription]);
};
objectLoader.onDidLoadResponse = ^(RKResponse *response) {
NSLog(#"response: %#", [response bodyAsString]);
};
}];
}

Send post request and map response to object

I am new to restKit and I have a few questions for you. I
cant understand how to send Post request using json/xml to my web
services and map the incoming reply with my classes. Can any one give
me a help on that. The code I am using is this:
in my applicationDelegate I am instantiating the RKObjectManager
providing the base URL:
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:#"https://example.com/services/"];
// Request params in Dictionary
NSArray *objects = [NSArray arrayWithObjects: email, password,
nil];
NSArray *keys = [NSArray arrayWithObjects:#"username",
#"password", nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
NSLog(#"Manager: %#", [RKObjectManager
sharedManager].description);
// User object Mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:
[User class]];
[userMapping mapKeyPath:#"userName" toAttribute:#"userName"];
[userMapping mapKeyPath:#"lastName" toAttribute:#"lastName"];
[userMapping mapKeyPath:#"active" toAttribute:#"active"];
[[RKObjectManager sharedManager].mappingProvider
setMapping:userMapping forKeyPath:#"user"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/
login" delegate:self];
When a post is send to /login the server should send back a valid json
and then map that json to my User class.
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:
(NSArray*)objects {
RKLogInfo(#"Load collection of Articles: %#", objects);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:
(NSError *)error
{
NSLog(#"Objecy Loader failed: %#", error);
}
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError
*)error
{
NSLog(#"Request failed");
}
- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(#"Retrieved XML: %#", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isXML]) {
///NSLog(#"Seng a JSON request:! \n %#", [request
HTTPBodyString]);
NSLog(#"Got a responce! \n %#", [response bodyAsString]);
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(#"The resource path '%#' was not found.", [request
resourcePath]);
}
}
}
When I execute it the objectLoader method are not triggered, my
understanding of restkit is that they should get called when
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/login"
delegate:self];
is executed ? Any help is appreciated :)
Well, you do not even send your data to the server. A couple of days ago, I wrote a snippet explaining how to post a NSDictionary (as JSON) to a server using RestKit.
did you remember to set the delegate for restkit to that user class (or where ever you have the delegates being caught) with
#interface User : NSObject<RKObjectLoaderDelegate>{
}
you probably did but its worth mentioning incase :P
and the self that gets passed in this line is that class?
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/
login" delegate:self];
also i think the newer way to add the mapping is and set up for posts is
[sharedManager.mappingProvider addObjectMapping:userMapping];
[sharedManager.mappingProvider setMapping:userMapping forKeyPath:#"/somepath"];
[sharedManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[User class]];
// Must set the router up to handle posts
[sharedManager.router routeClass:[User class] toResourcePath:#"/api/users" forMethod:RKRequestMethodPOST];
edit again : and create a post using the loader like this. probably all over kill for what your doing but cant hurt to have a look
RKObjectLoader* loader = [RKObjectLoader loaderWithResourcePath:url objectManager:self.objectManager delegate:self.currentDelegate];
loader.method = RKRequestMethodPOST;
loader.sourceObject = self;
loader.targetObject = self;
loader.serializationMIMEType = self.objectManager.serializationMIMEType;
loader.serializationMapping = [self.objectManager.mappingProvider serializationMappingForClass:self.mappingClass];
loader.objectMapping = [self.objectManager.mappingProvider objectMappingForClass:self.mappingClass];
[loader send]; //<<the actual post