RKObjectMappingProvider Class Reference
Inherits from | NSObject |
Declared in | RKObjectMappingProvider.h RKObjectMappingProvider.m |
Overview
The mapping provider is a repository of registered object mappings for use by instances of RKObjectManager and RKObjectMapper. It provides for the storage and retrieval of object mappings by keyPath and type.
The mapping provider is responsible for:
- Providing instances of RKObjectMapper with keyPaths and object mappings for use when attempting to map a parsed payload into objects. Each keyPath is examined using valueForKeyPath: to determine if any mappable data exists within the payload. If data is found, the RKObjectMapper will instantiate an RKObjectMappingOperation to perform the mapping using the RKObjectMapping or RKDynamicObjectMapping associated with the keyPath.
- Providing the appropriate serialization mapping to instances of RKObjectManager when an object is to be sent to the remote server using [RKObjectManager postObject:delegate:] or [RKObjectManager postObject:delegate]. This mapping is used to serialize the object into a format suitable for encoding into a URL form encoded or JSON representation.
- Providing convenient storage of RKObjectMapping references for users who are not using keyPath based mapping. Mappings can be added to the provider and retrieved by the [RKObjectMapping objectClass] that they target.
Provides extensions to RKObjectMappingProvider to support Core Data specific functionality.
Tasks
Other Methods
-
+ mappingProvider
Creates and returns an autoreleased RKObjectMappingProvider instance.
-
+ mappingProviderUsingBlock:
Instantiate and return a new auto-released object mapping provider after yielding it to the specified block for configuration
-
– setObjectMapping:forKeyPath:
Configures the mapping provider to use the RKObjectMapping or RKDynamicObjectMapping provided when content is encountered at the specified keyPath.
-
– objectMappingForKeyPath:
Returns the RKObjectMapping or RKObjectDynamic mapping configured for use when mappable content is encountered at keyPath
-
– removeObjectMappingForKeyPath:
Removes the RKObjectMapping or RKDynamicObjectMapping registered at the specified keyPath from the provider.
-
– objectMappingsByKeyPath
Returns a dictionary where the keys are mappable keyPaths and the values are the RKObjectMapping or RKDynamicObjectMapping to use for mappable data that appears at the keyPath.
-
– registerObjectMapping:withRootKeyPath:
Registers an object mapping as being rooted at a specific keyPath. The keyPath will be registered and an inverse mapping for the object will be generated and used for serialization.
-
– addObjectMapping:
Adds an object mapping to the provider for later retrieval. The mapping is not bound to a particular keyPath and must be explicitly set on an instance of RKObjectLoader or RKObjectMappingOperation to be applied. This is useful in cases where the remote system does not namespace resources in a keyPath that can be used for disambiguation.
-
– objectMappingsForClass:
Returns all object mappings registered for a particular class on the provider. The collection of mappings is assembled by searching for all mappings added via addObjctMapping: and then consulting those registered via objectMappingForKeyPath:
-
– objectMappingForClass:
Returns the first object mapping for a objectClass registered in the provider.
-
– setSerializationMapping:forClass:
Registers an object mapping for use when serializing instances of objectClass for transport over HTTP. Used by the object manager during postObject: and putObject:.
-
– serializationMappingForClass:
Returns the serialization mapping for a specific object class which has been previously registered.
-
– setObjectMapping:forResourcePathPattern:
Configures an object mapping to be used when during a load event where the resourcePath of the RKObjectLoader instance matches resourcePathPattern.
-
– objectMappingForResourcePath:
Returns the first objectMapping configured in the provider with a resourcePathPattern matching the specified resourcePath.
-
errorMapping
An object mapping used when the remote system returns an error status code and a payload with a MIME Type that RestKit is capable of parsing.
property -
paginationMapping
An object mapping used when mapping pagination metadata (current page, object count, etc) during a paginated object loading operation. The objectClass of the paginationMapping must be RKObjectPaginator.
property
Other Methods
-
– setMapping:context:
Stores a single object mapping for a given context. Useful when a component needs to enable configuration via one (and only one) object mapping.
CoreData Methods
-
– setObjectMapping:forResourcePathPattern:withFetchRequestBlock:
Configures an object mapping to be used when during a load event where the resourcePath of the RKObjectLoader instance matches resourcePathPattern.
-
– fetchRequestForResourcePath:
Retrieves the NSFetchRequest object that will retrieve cached objects for a given resourcePath.
Properties
errorMapping
An object mapping used when the remote system returns an error status code and a payload with a MIME Type that RestKit is capable of parsing.
@property (nonatomic, retain) RKObjectMapping *errorMapping
See Also
Declared In
RKObjectMappingProvider.h
paginationMapping
An object mapping used when mapping pagination metadata (current page, object count, etc) during a paginated object loading operation. The objectClass of the paginationMapping must be RKObjectPaginator.
@property (nonatomic, retain) RKObjectMapping *paginationMapping
Discussion
For example, if using the popular will_paginate plugin with Ruby on Rails, we would configure our pagination mapping like so:
// Assumes the JSON format of http://stackoverflow.com/questions/4699182/will-paginate-json-support RKObjectMapping *paginationMapping = [RKObjectMapping mappingForClass:[RKObjectPaginator class]]; [paginationMapping mapKeyPath:@“current_page” toAttribute:@“currentPage”]; [paginationMapping mapKeyPath:@“per_page” toAttribute:@“perPage”]; [paginationMapping mapKeyPath:@“total_entries” toAttribute:@“objectCount”];
See Also
Declared In
RKObjectMappingProvider.h
Class Methods
Instance Methods
addObjectMapping:
Adds an object mapping to the provider for later retrieval. The mapping is not bound to a particular keyPath and must be explicitly set on an instance of RKObjectLoader or RKObjectMappingOperation to be applied. This is useful in cases where the remote system does not namespace resources in a keyPath that can be used for disambiguation.
- (void)addObjectMapping:(RKObjectMapping *)objectMapping
Parameters
- objectMapping
An object mapping instance we wish to register with the provider.
Discussion
You can retrieve mappings added to the provider by invoking objectMappingsForClass: and objectMappingForClass:
Declared In
RKObjectMappingProvider.h
fetchRequestForResourcePath:
Retrieves the NSFetchRequest object that will retrieve cached objects for a given resourcePath.
- (NSFetchRequest *)fetchRequestForResourcePath:(NSString *)resourcePath
Parameters
- resourcePath
A resourcePath to retrieve the fetch request for.
Return Value
An NSFetchRequest object for fetching objects for the given resource path or nil.
Declared In
RKObjectMappingProvider+CoreData.h
objectMappingForClass:
Returns the first object mapping for a objectClass registered in the provider.
- (RKObjectMapping *)objectMappingForClass:(Class)objectClass
Parameters
- objectClass
The class that we want to return mappings for
Return Value
An RKObjectMapping matching objectClass or nil
Discussion
The objectClass is the class for a model you use to represent data retrieved in
XML or JSON format. For example, if we were developing a Twitter application we
might have an objectClass of RKTweet for storing Tweet data. We could retrieve
the object mapping for this model by invoking
[mappingProvider objectMappingForClass:[RKTweet class]];
Mappings registered via addObjectMapping: take precedence over those registered via setObjectMapping:forKeyPath:.
Declared In
RKObjectMappingProvider.h
objectMappingForKeyPath:
Returns the RKObjectMapping or RKObjectDynamic mapping configured for use when mappable content is encountered at keyPath
- (RKObjectMappingDefinition *)objectMappingForKeyPath:(NSString *)keyPath
Parameters
- keyPath
A registered keyPath to retrieve the object mapping for
Return Value
The RKObjectMapping or RKDynamicObjectMapping for the specified keyPath or nil if none is registered.
Declared In
RKObjectMappingProvider.h
objectMappingForResourcePath:
Returns the first objectMapping configured in the provider with a resourcePathPattern matching the specified resourcePath.
- (RKObjectMappingDefinition *)objectMappingForResourcePath:(NSString *)resourcePath
Parameters
- resourcePath
A resource path to retrieve the first RKObjectMapping or RKDynamicObjectMapping
configured with a matching pattern.
Return Value
An RKObjectMapping or RKDynamicObjectMapping for a resource path pattern matching resourcePath
or nil if no match was found.
Declared In
RKObjectMappingProvider.h
objectMappingsByKeyPath
Returns a dictionary where the keys are mappable keyPaths and the values are the RKObjectMapping or RKDynamicObjectMapping to use for mappable data that appears at the keyPath.
- (NSDictionary *)objectMappingsByKeyPath
Return Value
A dictionary of all registered keyPaths and their corresponding object mapping instances
Discussion
Warning: The returned dictionary can contain RKDynamicObjectMapping instances. Check the type if
you are using dynamic mapping.
Declared In
RKObjectMappingProvider.h
objectMappingsForClass:
Returns all object mappings registered for a particular class on the provider. The collection of mappings is assembled by searching for all mappings added via addObjctMapping: and then consulting those registered via objectMappingForKeyPath:
- (NSArray *)objectMappingsForClass:(Class)objectClass
Parameters
- objectClass
The class we want to retrieve the mappings for
Return Value
An array of all object mappings matching the objectClass. Can be empty.
Declared In
RKObjectMappingProvider.h
registerObjectMapping:withRootKeyPath:
Registers an object mapping as being rooted at a specific keyPath. The keyPath will be registered and an inverse mapping for the object will be generated and used for serialization.
- (void)registerObjectMapping:(RKObjectMapping *)objectMapping withRootKeyPath:(NSString *)keyPath
Parameters
- objectMapping
An object mapping we wish to register on the provider
- keyPath
The keyPath we wish to register for the mapping and use as the rootKeyPath for serialization
Discussion
This is a shortcut for configuring a pair of object mappings that model a simple resource the same way when going to and from the server.
For example, if we have a simple resource called ‘person’ that returns JSON in the following format:
{ "person": { "first_name": "Blake", "last_name": "Watters" } }
We might configure a mapping like so:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Person class]];
[mapping mapAttributes:@"first_name", @"last_name", nil];
If we want to parse the above JSON and serialize it such that using postObject: or putObject: use the same format, we can auto-generate the serialization mapping and set the whole thing up in one shot:
[[RKObjectManager sharedManager].mappingProvider registerMapping:mapping withRootKeyPath:@"person"];
This will call setMapping:forKeyPath: for you, then generate a serialization mapping and set the root keyPath as well.
If you want to manipulate the serialization mapping yourself, you can work with the mapping directly:
RKObjectMapping *serializationMappingForPerson = [personMapping inverseMapping];
// NOTE: Serialization mapping default to a nil root keyPath and will serialize to a flat dictionary
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:serializationMappingForPerson forClass:[Person class]];
Declared In
RKObjectMappingProvider.h
removeObjectMappingForKeyPath:
Removes the RKObjectMapping or RKDynamicObjectMapping registered at the specified keyPath from the provider.
- (void)removeObjectMappingForKeyPath:(NSString *)keyPath
Parameters
- keyPath
The keyPath to remove the corresponding mapping for
Declared In
RKObjectMappingProvider.h
serializationMappingForClass:
Returns the serialization mapping for a specific object class which has been previously registered.
- (RKObjectMapping *)serializationMappingForClass:(Class)objectClass
Parameters
- objectClass
The class we wish to obtain the serialization mapping for
Return Value
The RKObjectMapping instance used for mapping instances of objectClass for transport
See Also
Declared In
RKObjectMappingProvider.h
setMapping:context:
Stores a single object mapping for a given context. Useful when a component needs to enable configuration via one (and only one) object mapping.
- (void)setMapping:(RKObjectMappingDefinition *)mapping context:(RKObjectMappingProviderContext)context
Declared In
RKObjectMappingProvider+Contexts.h
setObjectMapping:forKeyPath:
Configures the mapping provider to use the RKObjectMapping or RKDynamicObjectMapping provided when content is encountered at the specified keyPath.
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectOrDynamicMapping forKeyPath:(NSString *)keyPath
Parameters
- objectOrDynamicMapping
An RKObjectMapping or RKDynamicObjectMapping to register for keyPath based mapping.
- keyPath
The keyPath to register the mapping as being responsible for mapping.
Discussion
When an RKObjectMapper is performing its work, each registered keyPath within the mapping provider will be searched for content in the parsed payload. If mappable content is found, the object mapping configured for the keyPath will be used to perform an RKObjectMappingOperation.
See Also
Declared In
RKObjectMappingProvider.h
setObjectMapping:forResourcePathPattern:
Configures an object mapping to be used when during a load event where the resourcePath of the RKObjectLoader instance matches resourcePathPattern.
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern
Parameters
- objectMapping
The object mapping to use when the resourcePath matches the specified
resourcePathPattern.
- resourcePathPattern
A pattern to be evaluated using an RKPathMatcher against a resourcePath
to determine if objectMapping is the appropriate mapping.
Discussion
The resourcePathPattern is a SOCKit pattern matching property names preceded by colons within a path. For example, if a collection of reviews for a product were loaded from a remote system at the resourcePath @“/products/1234/reviews”, object mapping could be configured to handle this request with a resourcePathPattern of @“/products/:productID/reviews”.
NOTE that care must be taken when configuring patterns within the provider. The patterns will be evaluated in the order they are added to the provider, so more specific patterns must precede more general patterns where either would generate a match.
See Also
Declared In
RKObjectMappingProvider.h
setObjectMapping:forResourcePathPattern:withFetchRequestBlock:
Configures an object mapping to be used when during a load event where the resourcePath of the RKObjectLoader instance matches resourcePathPattern.
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern withFetchRequestBlock:(RKObjectMappingProviderFetchRequestBlock)fetchRequestBlock
Parameters
- objectMapping
The object mapping to use when the resourcePath matches the specified resourcePathPattern.
- resourcePathPattern
A pattern to be evaluated using an RKPathMatcher against a resourcePath to determine if objectMapping is the appropriate mapping.
- fetchRequestBlock
A block that accepts an individual resourcePath and returns an NSFetchRequest that should be used to fetch the local objects associated with resourcePath from CoreData, for use in properly processing local deletes
Discussion
The resourcePathPattern is a SOCKit pattern matching property names preceded by colons within a path. For example, if a collection of reviews for a product were loaded from a remote system at the resourcePath @“/products/1234/reviews”, object mapping could be configured to handle this request with a resourcePathPattern of @“/products/:productID/reviews”.
NOTE that care must be taken when configuring patterns within the provider. The patterns will be evaluated in the order they are added to the provider, so more specific patterns must precede more general patterns where either would generate a match.
See Also
Declared In
RKObjectMappingProvider+CoreData.h
setSerializationMapping:forClass:
Registers an object mapping for use when serializing instances of objectClass for transport over HTTP. Used by the object manager during postObject: and putObject:.
- (void)setSerializationMapping:(RKObjectMapping *)objectMapping forClass:(Class)objectClass
Parameters
- objectMapping
The serialization mapping to register for use when serializing objectClass
- objectClass
The class of the object type we are registering a serialization for
Discussion
Serialization mappings are simply instances of RKObjectMapping that target NSMutableDictionary as the target object class. After the object is mapped into an NSMutableDictionary, it can be encoded to form encoded string, JSON, XML, etc.
Declared In
RKObjectMappingProvider.h