4

Restkit mapping and inserting data works fine, but I need to add custom values to the database (not from JSON)

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:entityName inManagedObjectStore:managedObjectStore];

[entityMapping addAttributeMappingsFromDictionary:dict];

if (uniqKey != nil) {
    entityMapping.identificationAttributes = @[ uniqKey ];
}

// Set MIME Type to JSON
manager.requestSerializationMIMEType = RKMIMETypeJSON;

// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:entityMapping
                                             method:RKRequestMethodPOST
                                        pathPattern:path
                                            keyPath:rootKeyPath
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

[manager addResponseDescriptor:responseDescriptor];

[manager postObject:nil path:path parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (mappingResult.array.count != 0) {
    NSDictionary *data = mappingResult.array[0];       
    NSLog(@"data: %@", data);      
}else{
    NSLog(@"Unable to fetch data from: %@", path);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error response': %@", error);
}];

Other than NSPredict and filtering the data, is is possible to insert values (like string) manually while mapping?

A7madev
  • 728
  • 3
  • 10
  • 18

2 Answers2

6

You can modify the objects from the mapping result in the completion block, but then you need to explicitly save the context and other observers of the context will have received a save notification. This is the super simple approach.

Alternatively you could override willSave or use NSManagedObjectContextWillSaveNotification (the latter being the better option) to trigger your custom logic. Your changes would then be made inline with the RestKit changes and would be automatically saved.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks a lot, are there any documentations or tutorials that could help with either approaches? – A7madev Sep 22 '14 at 07:31
  • I used willsave method in my NSManagedObject http://stackoverflow.com/a/5341212/2414731 but how can I save a value if the manager is in a viewController? – A7madev Sep 22 '14 at 07:54
  • The answers you linked to are a good resource for both MO approaches. I don't understand your question about the view controller. – Wain Sep 22 '14 at 08:13
  • I have the userID in my MainViewController, and Im calling Restkit manager (code above) in my MainViewController. The linked answer is only for static values inside the NSManagedObject class itself, what if I want to set value from a viewController or AppDelegate? – A7madev Sep 22 '14 at 08:27
2

Solved the issue using

[[mappingResult set] setValue:value forKey:key]; 

in manager on success block.

A7madev
  • 728
  • 3
  • 10
  • 18