Skip to content

Maps inserts, updates and deletions from your JSON response

License

Notifications You must be signed in to change notification settings

hyperoslo/NSManagedObject-ANDYMapChanges

 
 

Repository files navigation

NSManagedObject-ANDYMapChanges

CI Status Version License Platform

This is a category on NSManagedObject that helps you to evaluate insertions, deletions and updates by comparing your JSON dictionary with your CoreData local objects. It also provides uniquing for you locally stored objects.

The magic

+ (void)andy_mapChanges:(NSArray *)changes
              inContext:(NSManagedObjectContext *)context
          forEntityName:(NSString *)entityName
               inserted:(void (^)(NSDictionary *objectDict))inserted
                updated:(void (^)(NSDictionary *objectDict, NSManagedObject *object))updated;

How to use

- (void)importObjects:(NSArray *)objects usingContext:(NSManagedObjectContext *)context error:(NSError *)error
{
    [NSManagedObject andy_mapChanges:JSON
                           inContext:context
                       forEntityName:@"User"
                            inserted:^(NSDictionary *objectDict) {
                                ANDYUser *user = [ANDYUser insertInManagedObjectContext:context];
                                [user fillObjectWithAttributes:objectDict];
                            } updated:^(NSDictionary *objectDict, NSManagedObject *object) {
                                ANDYUser *user = (ANDYUser *)object;
                                [user fillObjectWithAttributes:objectDict];
                            }];

    [context save:&error];
}

Local and Remote keys

localKey is the name of the local primaryKey, if it's a user it could be remoteID. remoteKey is the name of the key from JSON, if it's a user it could be just id.

The convenience method that doesn't contain this attributes, fallsback to remoteID for the localKey and id for the remoteKey.

Predicate

Use the predicate to filter out mapped changes. For example if the JSON response belongs to only inactive users, you could have a predicate like this:

NSPredicate *predicate = [NSString stringWithFormat:@"inactive = YES"];

As a side note, you should use a fancier property mapper that does the fillObjectWithAttributes part for you.

Usage

To run the example project, clone the repo, and open the .xcodeproj from the Demo directory.

Requirements

iOS 7.0, Core Data

Installation

NSManagedObject-ANDYMapChanges is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'NSManagedObject-ANDYMapChanges'

Author

Elvis Nuñez, hello@nselvis.com

License

NSManagedObject-ANDYMapChanges is available under the MIT license. See the LICENSE file for more info.

About

Maps inserts, updates and deletions from your JSON response

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 84.8%
  • Shell 10.5%
  • Ruby 2.5%
  • C 2.2%