Recursive Object Mapping for Products & Categories using RestKit

E-commerce organizations today have started treating consumers (end-users) as purchase decision makers, instead of earlier focus on ‘buyer customers’. Most buying decisions in e-commerce are made by consumers, whether or not they physically make the purchase. Only the best offers with appropriate product quality levels, attract ‘consumer-customers’ regardless of transaction value.

Schemes, offers and cashbacks bring a challenging dynamic to the e-commerce domain, and can significantly influence the mindset of consumers looking to purchase items. Consumers often even compete with each other across peers for who will get the best offers, especially when product stocks are limited! This has forced sellers to re-think today’s fast evolving e-commerce platforms.

From SKUs ranging from smaller units of Sales, to high value, large volume lines from corporations with chain operations, sellers are planning for, or have already moved to performance oriented e-commerce platforms. Whereas the graph of e-commerce sales is increasing sharply on their web portals, to reach the maximum possible consumer-customers there is a clear need to effectively serve these users through mobile apps, especially in the case of impulse buying and point-of-decision purchases. Many companies have developed highly effective mobile apps as part of their e-commerce platforms, and they consistently upload and distribute their latest app versions through AppStore and PlayStore. Not only is iOS responsible for a large percentage of mobile usage and development, upto 76% of holiday sales last year in the US occured on iOS. With the launch of Apple Pay, iOS may gain larger chunks of non-holiday e-commerce sales versus competitors. iOS provides a number of good UI components using which developers can create sticky and compelling e-commerce apps.

What is required to create successful E-Commerce applications?

  1. Attractive and intuitive UI
  2. Web-Services for live data with recursive mapping (RestKit)
  3. Local data storage and synchronization
  • Attractive and intuitive UI
    • IOS has effective components for developers, and some controls are easily acquired from cocoa Controls.
    • For Products and items, UITableView and UICollectionView can be used to make nerd UIs for apps. Custom Views can be designed easily.
    • Drag and Drop features and Frame Layout make UI Development really fast.
  • Web-Services for live data with recursive mapping
    • For Web-Services, iOS offers many optimal options. iOS also offers HTTP connection methods like the recently introduced NSURLSession, using which a developer can easily execute http GET/POST methods & tcp requests using Block methods. But my all-time favorite SDK is RestKit, using which one can develop mapping based Request-Response using core data.
    • We frequently hear about recursive functions, which we at Zymr have implemented a few times. In e-Commerce apps, most of the Web part is generally covered by platforms like Magento. In Magento many features are part of the build, such as Product Object. In a Product hierarchy, a Product can recursively map itself with Product Object.

To develop Recursive Mapping we need to make a few modifications in Relationship in XCDatamodel. (Here i am assuming that you are aware of CoreData and XcDatamodel)
Develop an Entity as shown in the image below:

Develop

You may have noticed that we’ve created the same Relationship object in Inverse. This is a key aspect for Recursive Mapping. I have used RestKit, and RestKit integration methods are easily available on the web.

Now create NSManagedObject: Select Categories, Editor → Create NSManagedClass subclass, and there you go.

In Categories.m class we have to create an RKMapping object for Mapping, and need to apply recursive Mapping with the same Object.

+(RKEntityMapping *)objectMappingForCategory:(OPPCodeType)oppcode {
 RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:@"Categories" inManagedObjectStore:[[AppDelegate appDelegate] rkMOS]];

mapping.setDefaultValueForMissingAttributes = TRUE;
 if (oppcode == CATEGORY_LIST) {
 [mapping addAttributeMappingsFromDictionary:@{
 @"category_id" : @"category_id",
 @"is_active" : @"is_active",
 @"level" : @"level",
 @"parent_id" : @"parent_id",
 @"position" : @"position",
 @"name" : @"name",
 }];

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:mapping]];

}
 return mapping;
 }

The highlighted code is for mapping the same Object. While the Key Path is @”children”
that is the name of the relationship that we created in XCDatamodel. And the Mapping object is included.

Now that the mapping part is almost done, create RKObjectManager for Category (I am assuming that you have integrated RestKit)

In AppDelegate.h write:

@property (nonatomic, strong) RKObjectManager *rkomForCategory;
 In AppDelegate.m initialize rkomForCategory:
 rkomForCategory = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:kBase_URL]];
 [rkomForCategory setManagedObjectStore:rkMOS];
 [rkomForCategory addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:[[Categories objectMappingForCategory:CATEGORY_LIST] inverseMapping] objectClass:[Categories class] rootKeyPath:@"" method:RKRequestMethodPOST]];
 [rkomForCategory addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:[Categories objectMappingForCategory:CATEGORY_LIST] method:RKRequestMethodPOST pathPattern:nil keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

Now Category Objects has a property name “children” which has an array of Category. The Array occurs because we have set type as ToMany in XCDatamodel Relationship.
Now you can get Recursive Categories under Category Object.

  • Local data storage and synchronization
    • Core data is now widely use for local databases. It has a facility to deal with db as object, same as Hibernate in Java.
    • As per requirements, data can be easily stored and sync with central db.
This article was authored by Sitanshu Joshi, who is a Senior Software Engineer at Zymr.

0 comments

Leave a Reply

© 2019, Zymr, Inc. All Rights Reserved.| LEGAL DISCLAIMER | PRIVACY POLICY | COOKIE POLICY