SKProductsRequest已取消分配,即使使用ARC?

时间:2012-03-19 04:48:21

标签: iphone in-app-purchase automatic-ref-counting

我正在尝试设置SKProductsRequest,但我不断收到错误EXC_BAD_ACCESS。我知道这与ARC有关。 在我的.h文件中,我有SKProductsRequestDelegate。

这些是我的.m文件中的主要功能:

- (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];
}

当我启用NSZombieEnabled时,这就是我得到的:"-[InAppPurchaseManager respondsToSelector:]: message sent to deallocated instance."

非常感谢任何帮助。 谢谢!

2 个答案:

答案 0 :(得分:4)

我终于明白了!关键是当您合成变量时,请确保这样做:

@synthesize productsRequest = _productsRequest;

在.h中,它看起来应该是这样的:

@property (nonatomic, strong) SKProductsRequest *productsRequest;

在.m中,请务必使用" self。"使用productsRequest时:

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

你去吧!

答案 1 :(得分:1)

productsRequest[productsRequest start]

之后被立即销毁

您需要强制productsRequest的“保留”,方法是将其分配给__strong ivar或将其填入集合,字典或数组中。