Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restkit Add custom values when mapping

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?

like image 968
A7madev Avatar asked Feb 11 '23 23:02

A7madev


1 Answers

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.

like image 80
Wain Avatar answered Feb 16 '23 03:02

Wain