0
votes

Actually I've project in which I'm using RestKit 0.22.

In my code I've added object mapping:

RKEntityMapping *materialMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Material class])
                                                       inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
...
RKResponseDescriptor *responseMaterialsDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:materialMapping
                                                                                                 method:RKRequestMethodGET
                                                                                            pathPattern:urlMaterials
                                                                                                keyPath:kRestApiMAterialsKeyPath
                                                                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:responseMaterialsDescriptor];

Generally this code works OK, and after request proper url I get correct response and have stored objects to local database.

Now I want request the same url but after mapping JSON response to Material objects not store results to database. I want to insert all items after some modifications. Do you have any suggestions how can I avoid to store/save objects to DB by RestKit?

1
Why aren't you setting identification attributes and allowing RestKit to update existing items? Or you mean you only want to update existing items and ignore new items (in which case KVC validation is appropriate). - Wain

1 Answers

2
votes

If you wish to add some attributes or modify the existing attribute after mapping then you should use mappingMetadata and create responseDescriptors from it.

[materialMapping addAttributeMappingsFromDictionary:@{@"@metadata.property1": @"property1", @"@metadata.property2": @"property2"}];
RKResponseDescriptor *materialResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:materialResponseDescriptor pathPattern:@"somPath" keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

Then add the response descriptor to the RKObjectManager and create a RKManagedObjectRequestOperation for fetching from remote url,

  RKRoute *route = [[[[RKObjectManager sharedManager] router] routeSet] routeForName:routeName];
  [self cancelAllRequestWithMethod:route.method matchingPath:route.pathPattern];
  NSMutableURLRequest *request = [_objectManager requestWithPathForRouteNamed:routeName object:nil parameters:nil];
  RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:[[RKObjectManager sharedManager] responseDescriptors]];
  operation.managedObjectContext = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
  operation.managedObjectCache = [RKManagedObjectStore defaultStore].managedObjectCache;
  operation.savesToPersistentStore = YES;

Now, set the mapping metadata ie. this is the value that will be mapped to the original property while the mapping is being done,

[operation setMappingMetadata:@{@"property1": value1, @"property2": value2}];

Then, enqueue operation and it should map the custom value to the properties. Note, the property1 and property are the attribute in the entity and you do not create responseDescriptor for it.

[[RKObjectManager sharedManager] enqueueOperation:operation];