Skip to content

Commit

Permalink
See #19. Minor improves to usage of PromiseKit for createRecord:, rec…
Browse files Browse the repository at this point in the history
…ord:forId: and, push:withData:.
  • Loading branch information
Glavin001 committed Aug 22, 2014
1 parent 1781f63 commit 99e6bcc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
61 changes: 38 additions & 23 deletions Common/SLStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "SLStore.h"
#import "SLObjectIdTransform.h"
#import "CoreData+MagicalRecord.h"
#import <PromiseKit/Promise+When.h>

@interface SLStore ()
@end
Expand Down Expand Up @@ -44,12 +45,20 @@ - (instancetype) init
- (PMKPromise *) createRecord:(Class<SLModelProtocol>)modelClass withProperties:(NSDictionary *)properties
{
return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {

__block SLModel *record;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
SLModel *record = (SLModel *)[modelClass MR_createInContext:localContext];
record = (SLModel *)[modelClass MR_createInContext:localContext];
[record setupData:properties];
[localContext MR_saveToPersistentStoreAndWait];
record = [record MR_inContext:self.context];
fulfiller(record);
} completion:^(BOOL success, NSError *error) {
NSLog(@"%hhd %@ %@", success, error, record);
if (error) {
rejecter(error);
} else {
fulfiller(record);
}
}];
}];
}
Expand All @@ -59,9 +68,9 @@ - (PMKPromise *) record:(Class<SLModelProtocol>)modelClass forId:(SLNid)nid
{
return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {

__block SLModel *record;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

SLModel *record;
NSLog(@"record:forId:, before find node");
record = [modelClass MR_findFirstByAttribute:@"nid" withValue:nid inContext:localContext];
NSLog(@"record:forId: %@, node: %@", nid, record);
Expand All @@ -72,7 +81,13 @@ - (PMKPromise *) record:(Class<SLModelProtocol>)modelClass forId:(SLNid)nid
}
[localContext MR_saveToPersistentStoreAndWait];
record = [record MR_inContext:self.context];
fulfiller(record);
} completion:^(BOOL success, NSError *error) {
NSLog(@"%hhd %@ %@", success, error, record);
if (error) {
rejecter(error);
} else {
fulfiller(record);
}
}];

}];
Expand Down Expand Up @@ -210,27 +225,27 @@ - (PMKPromise *)saveRecord:(SLModel *)record

- (PMKPromise *) push:(Class)modelClass withData:(NSDictionary *)datum
{
NSLog(@"push: %@",datum);
return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {
//

NSLog(@"push: %@",datum);
SLNid nid = (NSString *) datum[@"nid"];

[self record:modelClass forId:nid]
.then(^(SLModel *record) {
[PMKPromise when:@[
[self record:modelClass forId:nid],
[self normalizeRelationships:modelClass withData:datum withStore:self]
]
]
.then(^(NSArray *results) {
SLModel *record = results[0];
NSDictionary *newDatum = results[1];
NSLog(@"record: %@", record);
NSLog(@"post normalizeRelationships datum: %@", newDatum);

[self normalizeRelationships:modelClass withData:datum withStore:self]
.then(^(NSDictionary *newDatum) {

NSLog(@"post normalizeRelationships datum: %@", newDatum);

//
[record setupData:newDatum];
NSLog(@"Pushed record: %@", record);

fulfiller(record);

})
.catch(rejecter);
//
[record setupData:newDatum];
NSLog(@"Pushed record: %@", record);

fulfiller(record);

})
.catch(rejecter);
Expand Down Expand Up @@ -263,7 +278,6 @@ - (PMKPromise *) pushMany:(Class)modelClass withData:(NSArray *)data
- (PMKPromise *)normalizeRelationships:(Class)modelClass withData:(NSDictionary *)data withStore:(SLStore *)store
{
return [PMKPromise new:^(PMKPromiseFulfiller fulfiller, PMKPromiseRejecter rejecter) {

NSLog(@"normalizeRelationships data: %@", data);
NSMutableDictionary *results = [NSMutableDictionary dictionaryWithDictionary:data];
NSDictionary *relationships = [modelClass relationshipsByName];
Expand Down Expand Up @@ -301,6 +315,7 @@ - (PMKPromise *)normalizeRelationships:(Class)modelClass withData:(NSDictionary
else {
val = [NSSet setWithArray:records];
}
NSLog(@"rel val: %@", val),
[results setValue:val forKey:relationshipKey];
// Load all of the records
[self findMany:relationshipModel withIds:ids];
Expand All @@ -314,7 +329,7 @@ - (PMKPromise *)normalizeRelationships:(Class)modelClass withData:(NSDictionary
PMKPromise *relPromise = [self deserializeRecordId:nid withRelationship:relationship withStore:store];
[relationshipPromises addObject:relPromise];
relPromise
.then(^(id val) {
.then(^(SLModel *val) {
[results setValue:val forKey:relationshipKey];
// Load this record
[self find:relationshipModel byId:nid];
Expand Down
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ target 'Streamlyne-iOS-SDK' do
platform :ios, '7.0'
pod 'AFNetworking', '~> 2.0'
pod 'MagicalRecord', '~> 2.2'
pod 'PromiseKit', '0.9.14'
pod 'PromiseKit'

#post_install do |installer|
# installer.project.targets.each do |target|
Expand All @@ -21,6 +21,6 @@ target 'Streamlyne-Mac-SDK' do
platform :osx, '10.9'
pod 'AFNetworking', '~> 2.0'
pod 'MagicalRecord', '~> 2.2'
pod 'PromiseKit', '0.9.14'
pod 'PromiseKit'

end

0 comments on commit 99e6bcc

Please sign in to comment.