I\'m using RestKit and i want to parse and save elements to core data. I have two json files:
First (Category):
[
{
\"cat_id\": 3371,
\"
I used RESTKit until the .20 release a few months ago. In all honesty, while this library has great intentions, in my experience it ends up wasting far more time than it was ever meant to save me.
I have used it with API's created with .NET/MVC, PHP, python/Django/TastyPie, and hacked it to do some stuff with Twitter. This all ended up being a very interesting academic exercise, but in the end was little more than that. Both the old/new versions of RESTKit assume ALOT about the way your API is going to be responding to requests.
With the .10 release, there was a fair amount of customizability through the use of Obj-C Blocks. I could intercept the request/response of an RKRequest and pretty much override anything that RESTKit was going to do. This seemed fairly awesome. Now with the .20 release, everything got very... clever (clever code is usually not good).
Instead of trying to remain generic and malleable, RK now does all kinds of "convenient" things for you. This effectively takes away your ability to jam it into whatever (non-idealized and realistic) shape your API has taken. At this point, I have gone back to writing all of my code using AFNetworking. I'd rather take the extra time to write my own parsing code, and know that when my Unit Tests pass, I'm done. NOT spend hours trying to get RK to log out meaningful error messages.
The other real problem with RK was that it does not account for being offline. It was designed with the assumption that your application is ALWAYS online, and simply mirrors a persistant store in the sky. My applications all handle content-creation on an iOS device, which MAY OR MAY NOT be online at the time of content-creation. This is the main reason I was "customizing" RK .10. When I saw the changes in .20, I decided enough was enough, and went back to doing things the old way.
Maybe I'll just write my own CRUD/RESTful framework, cause I'm starting to get tired of spending time using very specialized libraries, that try to do everything with as little responsibility being left to the application developer as possible. Frameworks like this tend to be Swiss Army Knives. They look really awesome on paper, THEY CAN DO ANYTHING, then you actually try to cut something and the blade is too dull or breaks off. Good software does very few things very well, and if it sounds too good to be true, it probably is.
So you've created the mapping for each NSManagedObject (and they work), but now you want to establish a relationship between "Category" and "Elements".
The problem is that there isn't an information for the mapping of Elements in which Category each element belongs (the unique key is missing). So you need to add the cat_id
to the Elements JSON to ensure that this keypath is available.
// Elements JSON
[
{
"cat_id": "1313",
"art_id": "1",
"node": {
"author": "name"
},
"small_url": 0
},
...
]
// Edit keypath in relationship mapping
[elementsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"cat_id" toKeyPath:@"category" withMapping:categoryMapping]];
In addition you could use the following to methods to track if the mapping works.
1) -com.apple.CoreData.SQLDebug 1 // Add in "Arguments" Tab > "Arguments Passed On Launch"
2) RKLogConfigureByName("RestKit/*", RKLogLevelTrace); // Add to AppDelegate`
Did you update the entity Elements
in your Data Model and your NSManagedObject with the given value? Seems like you didn't.
@interface Elements : NSManagedObject
@property (nonatomic, retain) NSNumber * catId;
@property (nonatomic, retain) NSNumber * artId;
@property (nonatomic, retain) NSString * author;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSManagedObject *category;
@end
The path your using for your get request won't work because @"/detailaddress/:catId"
isn't a valid path. You can on the one hand set the routing of the object manager in general or define a response description.
// Route for get operation
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute
routeWithClass:[Category class]
pathPattern:@"/detailaddress/:catId"
method:RKRequestMethodGET]];
// Response descriptor taken from the Core Data same application
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping pathPattern:@"/detailaddress/:catId" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
// Use general path for get request
[[RKObjectManager sharedManager] getObjectsAtPath:@"/detailaddress" ...
But please check if the mapping and data model is set like expected first.