Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem arises if</p> <pre><code>-(void)requestProdcutsWithCompletionHandler:(RequestProductsCompletionHandler) completionHandler{ .... { </code></pre> <p>is called twice before the first call completes. The reason is because the SKProductsRequestDelegate is used and calls the following function when it receives a response:</p> <pre><code>-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response{ ... _completionHandler(YES, availableProducts) _completionHandler = nil; } </code></pre> <p>Notice how _completionHandler is set to nil after the response is processed. Well, when the next response comes through from the duplicate call to "requestProductsWithCompletionHandler", _completionHandler is nil and throws the exception. You can fix this two ways, either make sure you do not call that function multiple times (bad way) or put some logic to make sure the completionHandler is not in use:</p> <pre><code>-(void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler{ NSLog(@"Request Products"); if (!_completionHandler) { NSLog(@"CompletionHandler called, new one was made"); _completionHandler = [completionHandler copy]; NSMutableSet * productIdentifiers = [NSMutableSet setWithCapacity:_products.count]; for (IAPProduct * product in _products.allValues) { product.availableForPurchase = NO; [productIdentifiers addObject:product.productIdentifier]; } _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; _productsRequest.delegate=self; [_productsRequest start]; }else{ NSLog(@"Duplicate call!"); } } </code></pre> <p>productRequest first checks to make sure _completionHanlder is nil, and if not then it ignores the request since the handler is in use.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload