Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

PlotGuru/PGMappingKit

Repository files navigation

Carthage Compatible Version License Platform

PGMappingKit helps you parsing a JSON response and getting it into Core Data. It is built on top of AFNetworking 3.

Requirements

  • iOS 8.0+
  • Xcode 7.2+

Installation

Carthage

Source is available through Carthage. To install it, add the following line to your Cartfile:

github "PlotGuru/PGMappingKit"  ~> 1.0

CocoaPods

Source is available through CocoaPods. To install it, add the following line to your Podfile:

pod 'PGMappingKit', '~> 1.0'

Example

Assuming you have the Core Data model shown in the image below:

Assuming you have the following JSON that can be retrieved from www.example.com/user:

[
  {
    "id": 6,
    "name": "Justin Jia",
    "posts": [
      {
        "id": 0,
        "text": "Hello, PGMappingKit!"
      }
    ]
  }
]

You can retrieve, parse the JSON response and get it into Core Data by using the following code:

// Initialize Network Handler

PGNetworkHandler *networkHandler = [PGNetworkHandler alloc] initWithBaseURL:[NSURL URLWithString:@"www.example.com"]];

// Describe Relationship

PGMappingDescription *postMapping = [PGMappingDescription name:@{@"posts": PGEntity(Post)}
                                                            ID:@{@"id": PGAttribute(Post, uniqueID)}
                                                       mapping:@{@"text": PGAttribute(Post, text)}];
PGMappingDescription *userMapping = [PGMappingDescription name:@{@"": PGEntity(User)}
                                                            ID:@{@"id": PGAttribute(User, uniqueID)}
                                                       mapping:@{@"name": PGAttribute(User, username),
                                                                 @"posts": postMapping}];

// Perform GET Request

[self.networkHandler GET:@"user"
                     from:nil
                     to:context
                     mapping:userMapping
                     option:PGSaveOptionUpdate
                     progress:nil
                     success:^(NSURLSessionDataTask * _Nonnull task, NSArray * _Nonnull results) {
        // Finish Login
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // Show Error
    } finish:^(NSURLSessionDataTask * _Nullable task) {
        // Refresh UI
}];

Usage

Use PGNetworkHandler.h like AFNetworking's AFHTTPSessionManager.h but with the additional finish block that will be called after either success or failure is finished.

- (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
                                  from:(nullable NSDictionary *)data
                               success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                               failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                                   from:(nullable NSDictionary *)data
                               progress:(nullable void (^)(NSProgress *progress))progress
                                success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                 finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                                  from:(nullable NSDictionary *)data
                              progress:(nullable void (^)(NSProgress *progress))progress
                               success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                               failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString
                                     from:(nullable NSDictionary *)data
                                  success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                  failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                   finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

Use PGNetworkHandler+PGCoreData.h like PGNetworkHandler.h but with the ability to save retrieved data to Core Data directly.

- (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
                                  from:(nullable NSDictionary *)data
                                    to:(NSManagedObjectContext *)context
                               mapping:(PGMappingDescription *)mapping
                                option:(PGSaveOption)option
                               success:(nullable void (^)(NSURLSessionDataTask *task, NSArray *results))success
                               failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                                   from:(nullable NSDictionary *)data
                                     to:(NSManagedObjectContext *)context
                                mapping:(PGMappingDescription *)mapping
                                 option:(PGSaveOption)option
                               progress:(nullable void (^)(NSProgress *progress))progress
                                success:(nullable void (^)(NSURLSessionDataTask *task, NSArray *results))success
                                failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                 finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                                  from:(nullable NSDictionary *)data
                                    to:(NSManagedObjectContext *)context
                               mapping:(PGMappingDescription *)mapping
                                option:(PGSaveOption)option
                              progress:(nullable void (^)(NSProgress *progress))progress
                               success:(nullable void (^)(NSURLSessionDataTask *task, NSArray *results))success
                               failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

- (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString
                                     from:(nullable NSDictionary *)data
                                       to:(NSManagedObjectContext *)context
                                  mapping:(PGMappingDescription *)mapping
                                   option:(PGSaveOption)option
                                  success:(nullable void (^)(NSURLSessionDataTask *task, NSArray *results))success
                                  failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure
                                   finish:(nullable void (^)(NSURLSessionDataTask * _Nullable task))finish;

Use PGNetworkHandler+PGData.h to generate a NSDictionary base on your NSManagedObject and use it as the request parameter (from key in the above APIs).

- (NSMutableDictionary *)dataFromObject:(nullable id)object mapping:(PGMappingDescription *)mapping;

Use the underlying NSManagedObjectContext+PGMapping.h to save arbitrary NSDictionary objects to your Core Data database.

- (id)save:(nullable NSDictionary *)data description:(PGMappingDescription *)mapping error:(NSError **)error;

- (id)save:(nullable NSDictionary *)data to:(id)object description:(PGMappingDescription *)mapping error:(NSError **)error;

Credits

PGMappingKit is owned by Plot Guru.

PGMappingKit was originally created by Justin Jia in the development of Plot Guru for iOS.

Licensing

PGMappingKit is released under the MIT license. See LICENSE for details.