RKMappingOperation Class Reference
Inherits from | NSOperation |
Declared in | RKMappingOperation.h |
Overview
Instances of RKMappingOperation
perform transformation between object representations according to the rules expressed in RKObjectMapping
objects. Mapping operations provide the foundation for the RestKit object mapping engine and perform the work of inspecting the attributes and relationships of a source object and determining how to map them into new representations on a destination object.
Metadata Mapping
The mapping operation provides support for mapping for a dictionary of metadata in addition to the source object. This metadata is made available by mapping key paths nested under a specially designated parent key that cannot exist in a source representation. By convention, metadata is typically nested under sub keys to effectively namespace usage between components. The object mapping engine itself reserves the ‘mapping’ key for its usage. Metadata is passed down through a hierarchy of mapping operations (i.e. as relationships are traversed), making a common set of ancillary information available for mapping for by any operation executed.
To understand how metadata works, consider the following example:
@interface RKMetadataExample : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSURL *URL;
@property (nonatomic, copy) NSDate *mappedAt;
@end
RKMetadataExample *example = [RKMetadataExample new];
NSDictionary *representation = @{ @"name": @"Blake Watters" };
NSDictionary *metadata = @{ @"URL": [NSURL URLWithString:@"http://restkit.org"]http://restkit.org"] };
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[RKMetadataExample class]];
[objectMapping addAttributeMappingsFromDictionary:@{ @"name": @"name", @"@metadata.URL": @"URL" }];
RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:representation destinationObject:example mapping:objectMapping];
mappingOperation.metadata = metadata;
NSError *error = nil;
BOOL success = [mappingOperation execute:&error];
Note the use of the special keyPath @"@
metadata.URL"
. The @
metadata prefix indicates that the property is to be mapped from the metadata dictionary instead of from the source object representation. If any relationships were mapped, it would have access to this same metadata information as well.
In addition to any metadata provided to the mapping operation via the metadata
property, the operation itself makes the following metadata key paths available for mapping:
@
metadata.
mapping.collectionIndex
– AnNSNumber
object specifying the index of the current object within a collection being mapped. This key is only available if the current representation exists within a collection.
Tasks
Initializing a Mapping Operation
-
– initWithSourceObject:destinationObject:mapping:
Initializes the receiver with a source object, a destination object and an object mapping with which to perform an object mapping.
Accessing Mapping Configuration
-
sourceObject
A dictionary of mappable elements containing simple values or nested object structures.
property -
destinationObject
The target object for this operation. Mappable values in the source object will be applied to the destination object using key-value coding.
property -
mapping
The mapping defining how values contained in the source object should be transformed to the destination object via key-value coding.
property -
objectMapping
The concrete object mapping for the operation.
property -
metadata
A dictionary of metadata available for mapping in addition to the source object.
property
Configuring Delegate and Data Source
-
delegate
The delegate to inform of interesting events during the mapping operation lifecycle.
property -
dataSource
The data source is responsible for providing the mapping operation with an appropriate target object for mapping when the
propertydestinationObject
isnil
.
Accessing Mapping Details
-
error
The error, if any, that occurred during the execution of the mapping operation.
property -
mappingInfo
Returns a dictionary containing information about the mappings applied during the execution of the operation. The keys of the dictionary are key paths into the
propertydestinationObject
for values that were mapped and the values are instances ofRKMappingDetails
that specify the object mapping and property mappings that were applied.
Performing Mapping
-
– performMapping:
Process all mappable values from the mappable dictionary and assign them to the target object according to the rules expressed in the object mapping definition
Properties
dataSource
The data source is responsible for providing the mapping operation with an appropriate target object for mapping when the destinationObject
is nil
.
@property (nonatomic, weak) id<RKMappingOperationDataSource> dataSource
Declared In
RKMappingOperation.h
delegate
The delegate to inform of interesting events during the mapping operation lifecycle.
@property (nonatomic, weak) id<RKMappingOperationDelegate> delegate
Declared In
RKMappingOperation.h
destinationObject
The target object for this operation. Mappable values in the source object will be applied to the destination object using key-value coding.
@property (nonatomic, strong, readonly) id destinationObject
Discussion
If initialized with a nil
destination object, the mapping operation will attempt to find or create a destination object via the data source and will populate the value of the destinationObject
property.
Declared In
RKMappingOperation.h
error
The error, if any, that occurred during the execution of the mapping operation.
@property (nonatomic, strong, readonly) NSError *error
Declared In
RKMappingOperation.h
mapping
The mapping defining how values contained in the source object should be transformed to the destination object via key-value coding.
@property (nonatomic, strong, readonly) RKMapping *mapping
Discussion
Will either be an instance of RKObjectMapping
or RKDynamicMapping
.
Declared In
RKMappingOperation.h
mappingInfo
Returns a dictionary containing information about the mappings applied during the execution of the operation. The keys of the dictionary are key paths into the destinationObject
for values that were mapped and the values are instances of RKMappingDetails
that specify the object mapping and property mappings that were applied.
@property (nonatomic, readonly) RKMappingInfo *mappingInfo
Discussion
Mapping info is aggregated for all child mapping operations executed for relationships.
Declared In
RKMappingOperation.h
metadata
A dictionary of metadata available for mapping in addition to the source object.
@property (nonatomic, copy) NSDictionary *metadata
Declared In
RKMappingOperation.h
objectMapping
The concrete object mapping for the operation.
@property (nonatomic, strong, readonly) RKObjectMapping *objectMapping
Discussion
If the value of mapping
is an RKObjectMapping
, returns the same value as mapping
. If mapping
is an RKDynamicMapping
, then returns the concrete RKObjectMapping
object selected for mapping sourceObject
.
Declared In
RKMappingOperation.h
Instance Methods
initWithSourceObject:destinationObject:mapping:
Initializes the receiver with a source object, a destination object and an object mapping with which to perform an object mapping.
- (id)initWithSourceObject:(id)sourceObject destinationObject:(id)destinationObject mapping:(RKMapping *)objectOrDynamicMapping
Parameters
- sourceObject
The source object to be mapped. Cannot be
nil
.
- destinationObject
The destination object the results are to be mapped onto. May be
nil
, in which case a new object target object will be obtained from thedataSource
.
- objectOrDynamicMapping
An instance of
RKObjectMapping
orRKDynamicMapping
defining how the mapping is to be performed.
Return Value
The receiver, initialized with a source object, a destination object, and a mapping.
Declared In
RKMappingOperation.h
performMapping:
Process all mappable values from the mappable dictionary and assign them to the target object according to the rules expressed in the object mapping definition
- (BOOL)performMapping:(NSError **)error
Parameters
Return Value
A Boolean value indicating if the mapping operation was successful.
Declared In
RKMappingOperation.h