应用程序与UITabBarController和In App Purchases崩溃

时间:2013-07-18 04:51:21

标签: ios uitabbarcontroller in-app-purchase

我在应用购买时使用的应用和我的推荐here

当我通过块从服务器加载产品时,同时我切换到UITabBarController中的其他选项卡,并且当加载产品时应用程序崩溃

这是我的代码

//Load products from server
[[LAInAppHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
    if (success) {
        // even i do nothing in here app till crashed
    }
}];

如果删除此行,我可以在任何标签之间切换。崩溃时没有任何东西被应用程序抛出,即使我启用了Zombie对象。只是访问不好

1 个答案:

答案 0 :(得分:3)

tutorial that you linkedLAInAppHelper的实现存在问题:帮助程序将您的应用程序视为非并发。

以下是:LAInAppHelper的共享实例有sharedInstance,拥有_completionHandler(以及其他内容)。

requestProductsWithCompletionHandler:方法为_completionHandler分配已传入的块的副本。对于第一个请求,这是可以的,但如果另一个请求是“在飞行中”,那么该完成块由于此次重新分配,ARC将发布其他请求。如果您切换到的选项卡启动并发请求,则初始请求将返回到已释放的块,从而导致未定义的行为,并可能导致崩溃。

要解决此问题,您需要将该类拆分为两个 - 一个部分包含所有请求共有的项(即_productIdentifiers_purchasedProductIdentifiers)和特定于请求的项({{1 }和_productsRequest)。

第一个类的实例(我们称之为_completionHandler)仍然是共享的;在LAInAppHelper方法中按请求创建第二个类的实例(我们称之为LAInAppHelperRequest)。

requestProductsWithCompletionHandler:

您还需要创建一个包裹-(id)initWithHelper:(LAInAppHelper*)helper andCompletionHandler:(RequestProductsCompletionHandler)completionHandler { if (self = [super init]) { _completionHandler = [completionHandler copy]; _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:helper.productIdentifiers]; // You will need to make productIdentifiers a property _productsRequest.delegate = self; [_productsRequest start]; } return self; } 的块,如下所示:

_completionHandler