Restkit 0.20 JSON Mapping Along with Additional Offline Data

Restkit 0.20 JSON Mapping along with additional offline data

Handle this outside RestKit, but in a way that is triggered by RestKit (and any other change):

Override willSave on your managed object subclass and update the modified date whenever it's called (setting the primitive value to avoid recursion).

CoreData mapping with JSON array with RestKit 0.20?

When you're adding the relationship mappings, the key paths in the mappings should be relative to the container, not relative to the root. Currently you have:

[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.User" toKeyPath:@"memberStatus" withMapping:userMapping]];
[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers.CirqitMember" toKeyPath:@"memberDetails" withMapping:membStatusMapping]];

Change that to:

[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"User" toKeyPath:@"memberStatus" withMapping:userMapping]];
[membMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"CirqitMember" toKeyPath:@"memberDetails" withMapping:membStatusMapping]];

This should also apply to:

[cirqitMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response.Cirqit.CirqitMembers" toKeyPath:@"cirqitMembers" withMapping:membMapping]];

But that one probably works because of the structure of the JSON...

Traverse upwards in JSON when mapping using RestKit 0.20

As of now (RestKit 0.20.1), there is no way to do this by using the mapping engine.

FUTURE

There is a new metadata feature under development making it possible to access the parent object:

[mapping addAttributeMappingsFromDictionary:@{ @"name": @"name", @"@metadata.parentObject.category": @"category" }];

PRESENT

I'm modifying my deserialized response using willMapDeserializedResponseBlock. I've added a category on RKObjectManager to make it easy to modify the response:

https://gist.github.com/gunnarblom/5677324

RestKit - Process one REST operation at a time

PROBLEM

RestKit uses multiple NSOperation for one REST operation, so all request mappings will be queued first with the code in the question. So when the first request mapping is executed and queuing the actual HTTP request, it gets queued behind the first two request mapping operations.

SOLUTION

Queue the next operation after the first one finishes.

Example with recursion:

- (void)sync {
NSArray *objectsToPostInOrder = ...;

for (id objectToPost in objectsToPostInOrder) {
[RKObjectManager.sharedManager postObject:objectToPost path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// Proceed with next if everything went OK
[self sync];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Don't continue posting as they are dependent on each other
[MyHUD showErrorWithStatus:error.localizedDescription];
}];

return;
}
}
}

Deserializing local NSString of JSON into objects via RestKit (no network download)

Pretty "simple":

NSString *stringJSON;
...

RKJSONParserJSONKit *parser;
NSError *error= nil;
parser= [[[RKJSONParserJSONKit alloc] init] autorelease];
MyManagedObject *target;
target= [MyManagedObject object];

NSDictionary *objectAsDictionary;
RKObjectMapper* mapper;
objectAsDictionary= [parser objectFromString:stringJSON error:&error];
mapper = [RKObjectMapper mapperWithObject:objectAsDictionary
mappingProvider:[RKObjectManager sharedManager].mappingProvider];
mapper.targetObject = target;
RKObjectMappingResult* result = [mapper performMapping];
NSLog(@"%@", [result asObject]);

Core-Data willSave: method

From the NSManagedObject docs for willSave:

If you want to update a persistent property value, you should typically test for equality of any new value with the existing value before making a change. If you change property values using standard accessor methods, Core Data will observe the resultant change notification and so invoke willSave again before saving the object’s managed object context. If you continue to modify a value in willSave, willSave will continue to be called until your program crashes.

For example, if you set a last-modified timestamp, you should check whether either you previously set it in the same save operation, or that the existing timestamp is not less than a small delta from the current time. Typically it’s better to calculate the timestamp once for all the objects being saved (for example, in response to an NSManagedObjectContextWillSaveNotification).

So maybe something along the lines of:

-(void)willSave {
NSDate *now = [NSDate date];
if (self.modificationDate == nil || [now timeIntervalSinceDate:self.modificationDate] > 1.0) {
self.modificationDate = now;
}
}

Where you can adjust the 1.0 to reflect the minimum delta between your expected save requests.



Related Topics



Leave a reply



Submit