Skip to content

Instantly share code, notes, and snippets.

@kyleclegg
Last active January 28, 2018 23:38
Show Gist options
  • Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.
Save kyleclegg/5846568 to your computer and use it in GitHub Desktop.
RestKit - Load data from local json file
NSString *filePath = [[NSBundle mainBundle] pathForResource:JohnSmith ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
KCUser *appUser = [[KCUser alloc] init];
NSString* MIMEType = @"application/json";
NSError* error;
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
NSLog(@"parser error");
}
NSDictionary *mappingsDictionary = @{ @"appUser": [KCUser mapping] };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
mapper.targetObject = appUser;
NSError *mappingError = nil;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
[[KCUserSingleton sharedKCUserSingleton] setUser:appUser];
}
else {
NSLog(@"error desc: %@", error.localizedDescription);
NSLog(@"error reason: %@", error.localizedFailureReason);
NSLog(@"error suggestion: %@", error.localizedRecoverySuggestion);
}
}
@kyleclegg
Copy link
Author

Shows how to load a json file locally. Assumes all mappings have been configured correctly.

@woodmister1
Copy link

on line 15, [KCUser mapping], how are you adding any relationships between user and for example sub RKObjectMapper types, as this is returning a dictionary, or can the dictionary contain @"key", [KCUser2 mapping]...

@kkotar
Copy link

kkotar commented Nov 28, 2013

woodminister1 try this:

Mappings Dictionary

The mappings dictionary describes how to object map the source object. The keys of the dictionary are key paths into the representation and the values are RKMapping objects describing how to map the representations at the corresponding key path. This dictionary based approach enables a single document to contain an arbitrary number of object representations that can be mapped independently. Consider the following example JSON structure:

{ "tags": [ "hacking", "phreaking" ], "authors": [ "Captain Crunch", "Emmanuel Goldstein" ], "magazine": { "title": "2600 The Hacker Quarterly" } }
Each key in the document could be mapped independently by providing a mapping for the key paths:

RKObjectMapping *tagMapping = [RKObjectMapping mappingForClass:[Tag class]];
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]];
RKObjectMapping *magazineMapping = [RKObjectMapping mappingForClass:[Magazine class]];
NSDictionary *mappingsDictionary = @{ @"tag": tagMapping, @"author": authorMapping, @"magazine": magazine };

more info : http://restkit.org/api/latest/Classes/RKMapperOperation.html

or you can use response descriptors from RKObjectManager:

NSMutableDictionary *mappingsDictionary = [[NSMutableDictionary alloc] init];
for (RKResponseDescriptor *descriptor in _objectManager.responseDescriptors) {
[mappingsDictionary setObject:descriptor.mapping forKey:descriptor.keyPath];
}

@kyleclegg
Copy link
Author

@woodmister1 - I like to setup my RestKit mappings within each model class rather than all at the time of configuration, which could get massive for a larger project. Then, I create a public class method to return the mapping for that class. Does that make sense? That is what I am calling on line 15. I can show my model class as well if you'd like.

@tolentinokas
Copy link

What is the targetObject for?

@slxl
Copy link

slxl commented Jan 28, 2018

thanks for the snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment