I am working on some IAP using this tutorial .
Firstly, I get the following products:
-(void)fetchAvailableProductsFirstLoad:(BOOL)firstTimeLoading { [[IAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) { ...
The assistant launches the following:
- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler { @synchronized(self) { // 1 _completionHandler = [completionHandler copy]; // 2 _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers]; _productsRequest.delegate = self; [_productsRequest start]; } }
When products return or fail, the following is called:
#pragma mark - SKProductsRequestDelegate - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { NSLog(@"Loaded list of products..."); _productsRequest = nil; NSArray * skProducts = response.products; for (SKProduct * skProduct in skProducts) { NSLog(@"Found product: %@ %@ %0.2f", skProduct.productIdentifier, skProduct.localizedTitle, skProduct.price.floatValue); } _completionHandler(YES, skProducts); _completionHandler = nil; } - (void)request:(SKRequest *)request didFailWithError:(NSError *)error { NSLog(@"Failed to load list of products."); NSLog(@"Error: %@",error); _productsRequest = nil; _completionHandler(NO, nil); _completionHandler = nil; }
Question
The problem is that the user runs the selection or products twice. For example, extraction products are called in viewDidLoad, but if the user has a poor / slow connection and the transition is made, then back to the controller. The original selection is not canceled, so two operations are performed.
I believe the problem is when the second is returned and the pointer has changed / does not exist / is damaged.
Error code EXC_BAD_ACCESS 2 in the corresponding line:
_completionHandler(YES, skProducts);
OR
_completionHandler(NO, nil);