Latest 0.7
Homepage https://github.com/nselvis/NSManagedObject-ANDYMapChanges
License MIT
Platforms ios 7.0, requires ARC
Dependencies NSManagedObject-ANDYObjectIDs
Frameworks Foundation, CoreData
Authors

DATAFilter

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
               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.

Operations

DATAFilter also provides the option to set which operations should be run when filtering, by default DATAFilterOperationAll is used but you could also set the option to just Insert and Update (avoiding removing items) or Update and Delete (avoiding updating items).

Usage goes like this:

// No items will be deleted here

[DATAFilter changes:JSON
      inEntityNamed:@"User"
          predicate:nil
         operations:DATAFilterOperationInsert | DATAFilterOperationUpdate
           localKey:@"remoteID"
          remoteKey:@"id"
            context:context
           inserted:^(NSDictionary *objectJSON) {
               // Do something with inserted items
           } updated:^(NSDictionary *objectJSON, NSManagedObject *updatedObject) {
               // Do something with updated items
           }];

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

License

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

Latest podspec

{
    "name": "NSManagedObject-ANDYMapChanges",
    "version": "0.7",
    "summary": "Maps inserts, updates and deletions from your JSON response",
    "description": "Maps inserts, updates and deletions from your JSON response.nnReturns every insert and update in a block, handles deletions and uniquing internally.n",
    "homepage": "https://github.com/nselvis/NSManagedObject-ANDYMapChanges",
    "license": "MIT",
    "authors": {
        "Elvis Nuu00f1ez": "[email protected]"
    },
    "source": {
        "git": "https://github.com/nselvis/NSManagedObject-ANDYMapChanges.git",
        "tag": "0.7"
    },
    "social_media_url": "https://twitter.com/nselvis",
    "platforms": {
        "ios": "7.0"
    },
    "requires_arc": true,
    "source_files": "Source/**/*",
    "frameworks": [
        "Foundation",
        "CoreData"
    ],
    "dependencies": {
        "NSManagedObject-ANDYObjectIDs": []
    }
}

Pin It on Pinterest

Share This