Here's my error...
Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)
Here is my product request delegate...
class ProductRequestDelegate : NSObject, SKProductsRequestDelegate {
private var products: ([SKProduct]) -> Void
init(products: @escaping ([SKProduct]) -> Void) {
self.products = products
}
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
products(response.products)
}
public func request(_ request: SKRequest, didFailWithError error: Error) {
}
}
Here is the request...
fileprivate var productsRequest: SKProductsRequest?
public func getProducts() -> Promise<Set<SKProduct>> {
return Promise { fulfill, reject in
productsRequest?.cancel()
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest!.delegate = ProductRequestDelegate { products in
self.productsRequest = nil
fulfill(Set(products))
}
productsRequest!.start()
}
}
This call results in the error occurring...
_ = iAPHelper.getProducts()
Any ideas why this should not work? It seems that the delegate is deallocated.
The delegate property on SKProductsRequest
is unowned(unsafe)
, so the object assigned to it is deallocated. (Instruments informed me of the deallocation after switching on zombies.)
So, I added a reference to the delegate to the class which contains the function getProducts()
. And it works.