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.
+ (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;
- (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];
}
localKey
is the name of the local primaryKey, for example remoteID
.
remoteKey
is the name of the key from JSON, for example id
.
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.
To run the example project, clone the repo, and open the .xcodeproj
from the Demo directory.
iOS 7.0
, Core Data
DATAFilter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'DATAFilter'
Elvis Nuñez, elvisnunez@me.com
DATAFilter is available under the MIT license. See the LICENSE file for more info.