Skip to content

Commit

Permalink
Hopefully Fix Issue hyochan#849
Browse files Browse the repository at this point in the history
Wrap every access to validProducts in an @synchronized-Block. This might impact the performance but it fixes the crashes described in Issue hyochan#849 for me.
  • Loading branch information
partiellkorrekt authored Mar 23, 2020
1 parent 9757602 commit a1cb2e3
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions ios/RNIapIos.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
reject:(RCTPromiseRejectBlock)reject) {
pendingTransactionWithAutoFinish = finishAutomatically;
SKProduct *product;
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
@synchronized (validProducts) {
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
}
}
}
if (product) {
Expand Down Expand Up @@ -174,10 +176,12 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
reject:(RCTPromiseRejectBlock)reject) {
SKProduct *product;
SKMutablePayment *payment;
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
@synchronized (validProducts) {
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
}
}
}
if (product) {
Expand Down Expand Up @@ -219,10 +223,12 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
reject:(RCTPromiseRejectBlock)reject) {
NSLog(@"\n\n\n buyProductWithQuantityIOS \n\n.");
SKProduct *product;
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
@synchronized (validProducts) {
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
}
}
}
if (product) {
Expand Down Expand Up @@ -253,7 +259,9 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ

RCT_EXPORT_METHOD(clearProducts) {
NSLog(@"\n\n\n *** clear valid products. \n\n.");
[validProducts removeAllObjects];
@synchronized (validProducts) {
[validProducts removeAllObjects];
}
}

RCT_EXPORT_METHOD(promotedProduct:(RCTPromiseResolveBlock)resolve
Expand Down Expand Up @@ -324,8 +332,11 @@ -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProduc
}
NSMutableArray* items = [NSMutableArray array];

for (SKProduct* product in validProducts) {
[items addObject:[self getProductObject:product]];

@synchronized (validProducts) {
for (SKProduct* product in validProducts) {
[items addObject:[self getProductObject:product]];
}
}

[self resolvePromisesForKey:RCTKeyForInstance(request) value:items];
Expand All @@ -334,18 +345,20 @@ -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProduc
// Add to valid products from Apple server response. Allowing getProducts, getSubscriptions call several times.
// Doesn't allow duplication. Replace new product.
-(void)addProduct:(SKProduct *)aProd {
NSLog(@"\n Add new object : %@", aProd.productIdentifier);
int delTar = -1;
for (int k = 0; k < validProducts.count; k++) {
SKProduct *cur = validProducts[k];
if ([cur.productIdentifier isEqualToString:aProd.productIdentifier]) {
delTar = k;
@synchronized (validProducts) {
NSLog(@"\n Add new object : %@", aProd.productIdentifier);
int delTar = -1;
for (int k = 0; k < validProducts.count; k++) {
SKProduct *cur = validProducts[k];
if ([cur.productIdentifier isEqualToString:aProd.productIdentifier]) {
delTar = k;
}
}
if (delTar >= 0) {
[validProducts removeObjectAtIndex:delTar];
}
[validProducts addObject:aProd];
}
if (delTar >= 0) {
[validProducts removeObjectAtIndex:delTar];
}
[validProducts addObject:aProd];
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
Expand Down

0 comments on commit a1cb2e3

Please sign in to comment.