Skip to content

Commit

Permalink
Merge pull request #13 from NSElvis/improve/persist-when-parsing-fini…
Browse files Browse the repository at this point in the history
…shed

Persist when processing finishes
  • Loading branch information
NSElvis committed Feb 17, 2015
2 parents 1d2e6b0 + cbd55ff commit 9839065
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Kipu.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Kipu"
s.version = "0.4.1"
s.version = "0.4.2"
s.summary = "A cape, a sword and networking superpowers for your Core Data models"
s.description = <<-DESC
This is a category that eases your every day job of parsing an API and getting it into Core Data.
Expand Down
7 changes: 0 additions & 7 deletions Source/Kipu.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,4 @@
dataStack:(DATAStack *)dataStack
completion:(void (^)(NSError *error))completion;

+ (void)processChanges:(NSArray *)changes
usingEntityName:(NSString *)entityName
predicate:(NSPredicate *)predicate
parent:(NSManagedObject *)parent
inContext:(NSManagedObjectContext *)context
completion:(void (^)(NSError *error))completion;

@end
22 changes: 16 additions & 6 deletions Source/Kipu.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ - (NSManagedObject *)kipu_copyInContext:(NSManagedObjectContext *)context;
- (NSArray *)kipu_relationships;

- (void)kipu_processRelationshipsUsingDictionary:(NSDictionary *)objectDict
andParent:(NSManagedObject *)parent;
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack;

- (void)kipu_processToManyRelationship:(NSRelationshipDescription *)relationship
usingDictionary:(NSDictionary *)objectDict
andParent:(NSManagedObject *)parent;
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack;

- (void)kipu_processToOneRelationship:(NSRelationshipDescription *)relationship
usingDictionary:(NSDictionary *)objectDict;
Expand Down Expand Up @@ -50,6 +52,7 @@ + (void)processChanges:(NSArray *)changes
predicate:predicate
parent:nil
inContext:backgroundContext
dataStack:dataStack
completion:completion];
}];
}
Expand All @@ -70,6 +73,7 @@ + (void)processChanges:(NSArray *)changes
predicate:predicate
parent:safeParent
inContext:backgroundContext
dataStack:dataStack
completion:completion];
}];
}
Expand All @@ -79,6 +83,7 @@ + (void)processChanges:(NSArray *)changes
predicate:(NSPredicate *)predicate
parent:(NSManagedObject *)parent
inContext:(NSManagedObjectContext *)context
dataStack:(DATAStack *)dataStack
completion:(void (^)(NSError *error))completion
{
[NSManagedObject andy_mapChanges:changes
Expand All @@ -90,20 +95,22 @@ + (void)processChanges:(NSArray *)changes
NSManagedObject *created = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
[created hyp_fillWithDictionary:objectDict];
[created kipu_processRelationshipsUsingDictionary:objectDict andParent:parent];
[created kipu_processRelationshipsUsingDictionary:objectDict andParent:parent dataStack:dataStack];

} updated:^(NSDictionary *objectDict, NSManagedObject *object) {

[object hyp_fillWithDictionary:objectDict];
[object kipu_processRelationshipsUsingDictionary:objectDict andParent:parent];
[object kipu_processRelationshipsUsingDictionary:objectDict andParent:parent dataStack:dataStack];

}];

NSError *error = nil;
[context save:&error];
if (error) NSLog(@"Kipu (error while saving %@): %@", entityName, [error description]);

if (completion) completion(error);
[dataStack persistWithCompletion:^{
if (completion) completion(error);
}];
}

+ (NSManagedObject *)safeObjectInContext:(NSManagedObjectContext *)context
Expand Down Expand Up @@ -148,12 +155,13 @@ - (NSArray *)kipu_relationships

- (void)kipu_processRelationshipsUsingDictionary:(NSDictionary *)objectDict
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack
{
NSArray *relationships = [self kipu_relationships];

for (NSRelationshipDescription *relationship in relationships) {
if (relationship.isToMany) {
[self kipu_processToManyRelationship:relationship usingDictionary:objectDict andParent:parent];
[self kipu_processToManyRelationship:relationship usingDictionary:objectDict andParent:parent dataStack:dataStack];
} else {
if (parent) {
[self setValue:parent forKey:relationship.name];
Expand All @@ -167,6 +175,7 @@ - (void)kipu_processRelationshipsUsingDictionary:(NSDictionary *)objectDict
- (void)kipu_processToManyRelationship:(NSRelationshipDescription *)relationship
usingDictionary:(NSDictionary *)objectDict
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack
{
NSString *childEntityName = relationship.destinationEntity.name;
NSString *parentEntityName = parent.entity.name;
Expand Down Expand Up @@ -207,6 +216,7 @@ - (void)kipu_processToManyRelationship:(NSRelationshipDescription *)relationship
predicate:childPredicate
parent:self
inContext:self.managedObjectContext
dataStack:dataStack
completion:nil];
}

Expand Down

0 comments on commit 9839065

Please sign in to comment.