SKProductsRequest released while using ARC?

I am trying to set up SKProductsRequest but I keep getting EXC_BAD_ACCESS error. I know this has to do with ARC. In my .h file, I have an SKProductsRequestDelegate.

These are the main functions in my .m file:

- (void)requestProUpgradeProductData {
    NSSet *productIdentifiers = [NSSet setWithObject:kInAppPurchaseProUpgradeProductId];
    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];
}

#pragma mark -
#pragma mark SKProductsRequestDelegate methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *products = response.products;
    //proUpgradeProduct = [products count] == 1 ? [products firstObject] : nil;
    proUpgradeProduct = [products objectAtIndex:0];
    if (proUpgradeProduct) {
        NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
        NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
        NSLog(@"Product price: %@" , proUpgradeProduct.price);
        NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
    }

    for (NSString *invalidProductId in response.invalidProductIdentifiers) {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    // finally release the reqest we alloc/init’ed in requestProUpgradeProductData
    productsRequest = nil;

    [self purchaseProUpgrade];

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

      

When I enable NSZombieEnabled, this is what I get: "-[InAppPurchaseManager respondsToSelector:]: message sent to deallocated instance."

Any help would be greatly appreciated. Thank!

+3


source to share


2 answers


I finally figured it out! The key is when you synthesize a variable, remember to do it like this:

@synthesize productsRequest = _productsRequest;

      

And in .h it should look like this:

@property (nonatomic, strong) SKProductsRequest *productsRequest;

      



In .m, make sure you are using "i". when using productsRequest:

self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
self.productsRequest.delegate = self;
[self.productsRequest start];

      

There you go!

+4


source


productsRequest

destroyed immediately after [productsRequest start]



You need to force "save" productsRequest

by assigning it to an __strong

ivar or adding it to a set, dict, or array.

+1


source







All Articles