/DATAFilter

Filter inserts, updates and deletions from your JSON response

Primary LanguageObjective-COtherNOASSERTION

DATAFilter

CI Status Version License Platform

Helps you filter insertions, deletions and updates by comparing your JSON dictionary with your Core Data local objects. It also provides uniquing for you locally stored objects and automatic removal of not found ones.

The magic

+ (void)changes:(NSArray *)changes
  inEntityNamed:(NSString *)entityName
       localKey:(NSString *)localKey
      remoteKey:(NSString *)remoteKey
        context:(NSManagedObjectContext *)context
       inserted:(void (^)(NSDictionary *objectJSON))inserted
        updated:(void (^)(NSDictionary *objectJSON, NSManagedObject *updatedObject))updated;

How to use

- (void)importObjects:(NSArray *)JSON usingContext:(NSManagedObjectContext *)context error:(NSError *)error
{
    [DATAFilter changes:JSON
          inEntityNamed:@"User"
               localKey:@"remoteID"
              remoteKey:@"id"
                context:context
              predicate:nil
               inserted:^(NSDictionary *objectJSON) {
                    ANDYUser *user = [ANDYUser insertInManagedObjectContext:context];
                    [user fillObjectWithAttributes:objectDict];
              } updated:^(NSDictionary *objectJSON, NSManagedObject *updatedObject) {
                    ANDYUser *user = (ANDYUser *)object;
                    [user fillObjectWithAttributes:objectDict];
              }];

    [context save:&error];
}

Local and Remote keys

localKey is the name of the local primaryKey, for example remoteID.
remoteKey is the name of the key from JSON, for example id.

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

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

pod 'DATAFilter'

Author

Elvis Nuñez, elvisnunez@me.com

License

DATAFilter is available under the MIT license. See the LICENSE file for more info.