Created
July 2, 2012 14:42
-
-
Save acf/3033572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[MagicalRecord saveWithBlock:^(NSManagedObjectContext* localContext) { | |
LSUser* currentUser = nil; | |
NSDictionary* apiKeyDic = [[ACSimpleKeychain defaultKeychain] credentialsForIdentifier:@"api_key" service:@"MerchantCenter"]; | |
if( apiKeyDic != nil ) { | |
NSString* email = [apiKeyDic objectForKey:ACKeychainUsername]; | |
if( email != nil ) { | |
currentUser = [LSUser MR_findFirstByAttribute:LSUserAttributes.email withValue:email inContext:localContext]; | |
NSLog(@"USER HAS VOUCHERS: %ld", [LSVoucher MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"user = %@", currentUser]]); | |
} | |
} | |
NSArray* items = [responseObject objectForKey:@"vouchers"]; | |
NSArray* vouchers = [LSVoucher MR_importFromArray:items inContext:localContext]; | |
for(LSVoucher* v in vouchers) { | |
if( [[v.user objectID] isEqual:[currentUser objectID]] == NO) | |
v.user = currentUser; | |
} | |
}]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSManagedObjectContext* ctx = [NSManagedObjectContext MR_contextThatPushesChangesToDefaultContext]; | |
LSUser* currentUser = nil; | |
NSDictionary* apiKeyDic = [[ACSimpleKeychain defaultKeychain] credentialsForIdentifier:@"api_key" service:@"MerchantCenter"]; | |
if( apiKeyDic != nil ) { | |
NSString* email = [apiKeyDic objectForKey:ACKeychainUsername]; | |
if( email != nil ) { | |
currentUser = [LSUser MR_findFirstByAttribute:LSUserAttributes.email withValue:email inContext:ctx]; | |
NSLog(@"USER HAS VOUCHERS: %ld", [LSVoucher MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"user = %@", currentUser]]); | |
} | |
} | |
NSArray* items = [responseObject objectForKey:@"vouchers"]; | |
for(NSDictionary* item in items) { | |
LSVoucher* v = [LSVoucher MR_importFromObject:item inContext:ctx]; | |
if( [[v.user objectID] isEqual:[currentUser objectID]] == NO) | |
v.user = currentUser; | |
[ctx MR_save]; | |
NSLog(@"."); | |
} | |
[ctx MR_saveNestedContexts]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment