-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance by caching keychain transactions
- Loading branch information
Showing
1 changed file
with
29 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,39 +84,41 @@ void RMKeychainSetValue(NSData *value, NSString *key) | |
return (__bridge NSData*)value; | ||
} | ||
|
||
@implementation RMStoreKeychainPersistence | ||
@implementation RMStoreKeychainPersistence { | ||
NSDictionary *_transactionsDictionary; | ||
} | ||
|
||
#pragma mark - RMStoreTransactionPersistor | ||
|
||
- (void)persistTransaction:(SKPaymentTransaction*)paymentTransaction | ||
{ | ||
SKPayment *payment = paymentTransaction.payment; | ||
NSString *productIdentifier = payment.productIdentifier; | ||
NSDictionary *transactions = [RMStoreKeychainPersistence transactionsDictionary]; | ||
NSDictionary *transactions = [self transactionsDictionary]; | ||
NSInteger count = [[transactions objectForKey:productIdentifier] integerValue]; | ||
count++; | ||
NSMutableDictionary *updatedTransactions = [NSMutableDictionary dictionaryWithDictionary:transactions]; | ||
[updatedTransactions setObject:@(count) forKey:productIdentifier]; | ||
[RMStoreKeychainPersistence setTransactionsDictionary:updatedTransactions]; | ||
[self setTransactionsDictionary:updatedTransactions]; | ||
} | ||
|
||
#pragma mark - Public | ||
|
||
- (void)removeTransactions | ||
{ | ||
[RMStoreKeychainPersistence setTransactionsDictionary:nil]; | ||
[self setTransactionsDictionary:nil]; | ||
} | ||
|
||
- (BOOL)consumeProductOfIdentifier:(NSString*)productIdentifier | ||
{ | ||
NSDictionary *transactions = [RMStoreKeychainPersistence transactionsDictionary]; | ||
NSDictionary *transactions = [self transactionsDictionary]; | ||
NSInteger count = [[transactions objectForKey:productIdentifier] integerValue]; | ||
if (count > 0) | ||
{ | ||
count--; | ||
NSMutableDictionary *updatedTransactions = [NSMutableDictionary dictionaryWithDictionary:transactions]; | ||
[updatedTransactions setObject:@(count) forKey:productIdentifier]; | ||
[RMStoreKeychainPersistence setTransactionsDictionary:updatedTransactions]; | ||
[self setTransactionsDictionary:updatedTransactions]; | ||
return YES; | ||
} else { | ||
return NO; | ||
|
@@ -125,43 +127,49 @@ - (BOOL)consumeProductOfIdentifier:(NSString*)productIdentifier | |
|
||
- (NSInteger)countProductOfdentifier:(NSString*)productIdentifier | ||
{ | ||
NSDictionary *transactions = [RMStoreKeychainPersistence transactionsDictionary]; | ||
NSDictionary *transactions = [self transactionsDictionary]; | ||
NSInteger count = [[transactions objectForKey:productIdentifier] integerValue]; | ||
return count; | ||
} | ||
|
||
- (BOOL)isPurchasedProductOfIdentifier:(NSString*)productIdentifier | ||
{ | ||
NSDictionary *transactions = [RMStoreKeychainPersistence transactionsDictionary]; | ||
NSDictionary *transactions = [self transactionsDictionary]; | ||
return [transactions objectForKey:productIdentifier] != nil; | ||
} | ||
|
||
- (NSSet*)purchasedProductIdentifiers | ||
{ | ||
NSDictionary *transactions = [RMStoreKeychainPersistence transactionsDictionary]; | ||
NSDictionary *transactions = [self transactionsDictionary]; | ||
NSArray *productIdentifiers = [transactions allKeys]; | ||
return [NSSet setWithArray:productIdentifiers]; | ||
} | ||
|
||
#pragma mark - Private | ||
|
||
+ (NSDictionary*)transactionsDictionary | ||
- (NSDictionary*)transactionsDictionary | ||
{ | ||
NSData *data = RMKeychainGetValue(RMStoreTransactionsKeychainKey); | ||
NSDictionary *transactions; | ||
if (data) | ||
{ | ||
NSError *error; | ||
transactions = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | ||
NSAssert(!error, [error localizedDescription]); | ||
} else { | ||
transactions = [NSDictionary dictionary]; | ||
if (_transactionsDictionary) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hpique
Author
Member
|
||
{ // Reading the keychain is slow so we cache its values in memory | ||
NSData *data = RMKeychainGetValue(RMStoreTransactionsKeychainKey); | ||
NSDictionary *transactions; | ||
if (data) | ||
{ | ||
NSError *error; | ||
transactions = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | ||
NSAssert(!error, [error localizedDescription]); | ||
} else { | ||
transactions = [NSDictionary dictionary]; | ||
} | ||
_transactionsDictionary = transactions; | ||
} | ||
return transactions; | ||
return _transactionsDictionary; | ||
|
||
} | ||
|
||
+ (void)setTransactionsDictionary:(NSDictionary*)dictionary | ||
- (void)setTransactionsDictionary:(NSDictionary*)dictionary | ||
{ | ||
_transactionsDictionary = dictionary; | ||
NSData *data = nil; | ||
if (dictionary) | ||
{ | ||
|
In order to cache, shouldn't this read:
if (!_transactionsDictionary)