NSManagedObject-ANDYMapChanges 0.7

NSManagedObject-ANDYMapChanges 0.7

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2015

Maintained by Elvis Nuñez.



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, [email protected]

License

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