Restkit and Saving to Coredata as Nsmanagedobject

RestKit and saving to CoreData as NSManagedObject

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.

RestKit - saving JSON content into a string coredata attribute

Would it be possible to save the JSON content of customData into a string coredata attribute?

No, because it will be deserialised to a dictionary and there is no converter for that to a string.

You can store it as a dictionary. It you could add a relationship mapping with a dynamic mapping which checks what the keys are and defines the mapping on the fly...

Restkit mapping arrays to core data

For anyone trying to find a solution to this problem:

The problem was that I was using an Object Mapping with Core Data.
I changed all references to an Object Mapping into Entity Mappings instead. The relationship mapping is the same but now points to the Core Data entity instead of the class. Now everything works as expected.

RKEntityMapping *storeMapping = [RKEntityMapping mappingForEntityForName:@"Store" inManagedObjectStore:managedObjectStore];
storeMapping.identificationAttributes = @[@"id"];
[storeMapping addAttributeMappingsFromArray:@[ @"id", @"editable", @"number", @"name", @"banner", @"address", @"city", @"contact", @"latitude", @"longitude", @"banner_id"]];

RKEntityMapping *hoursMapping = [RKEntityMapping mappingForEntityForName:@"Hours" inManagedObjectStore:managedObjectStore];
[hoursMapping addAttributeMappingsFromArray:@[@"day", @"start", @"end"]];
[storeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hours" toKeyPath:@"hours" withMapping:hoursMapping]];

RKResponseDescriptor *storeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping
method:RKRequestMethodAny
pathPattern:@"/api/v2/stores.json"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:storeResponseDescriptor];

For more information visit: https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core-data-9326af750e10

How to save to Core Data RKMappingResult object in RestKit?

You need to take a step back as your suggestions are shooting in the dark.

If you configure your object manager with a managed object store and create response descriptors with entity mappings then when you receive data as a result of requests this will be converted into managed objects. These objects will automatically be saved to the core data store before the success block is called.

Any other objects you want to create can be created as usual and you need to explicitly save the context.

Sending requests with RestKit doesn't itself change the store contents, only the response results in changes.

Core data with Restkit

I think you may need add a key like createAt
I remember you can keep data in order in CoreData

Also see this question: iPhone: NSSet keep sort order

GET object and relating it to a previously saved object using RestKit and CoreData

My bad, I didn't go through the documentation enough. Here's how to do it (based on this stackoverflow's answer):

NSEntityDescription *eventEntity = [NSEntityDescription entityForName:NSStringFromClass([Event class]) inManagedObjectContext:store.mainQueueManagedObjectContext];
NSRelationshipDescription *categoryRelationship = [eventEntity relationshipsByName][@"category"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:categoryRelationship attributes:@{ @"categoryID": @"categoryID" }];

//entity mapping is the event's mapping like: RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName...
[entityMapping addConnection:connection];

The relationship "category" must be configured properly in Core Data.

For me that did the trick.



Related Topics



Leave a reply



Submit