enumerateObjectsUsingBlock: Not Executed



这里是我称它的地方:

/**
*  called when we are notified that a product has been purchased
*
*  @param  notification                the notification object calling this method
*/
- (void)productPurchased:(NSNotification *)notification
{
//  the notification object is purchased product identifier
NSString *productIdentifier         = notification.object;
NSLog(@"Product purchased");
//  reload appropriate cell with checkmark
[_products enumerateObjectsUsingBlock:^(SKProduct *product, NSUInteger idx, BOOL *stop)
{
NSLog(@"Block executing with product: %@", product.productIdentifier);
NSLog(@"Needs to match: %@", productIdentifier);
if ([product.productIdentifier isEqualToString:productIdentifier])
{
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"Reloading due to purchase");
*stop                       = YES;
}
}];
}

但是,永远不会调用块,因此永远不会重新加载表视图单元。我可以看出,该块从未执行过,因为日志从未显示在控制台中。但是,通过放置断点,我已经能够确定productPurchase:方法肯定被调用了。

感谢您的帮助。

谢谢。

编辑:我现在已经更改了上面的代码,以便在执行过程中进行更多的日志记录。根据我通过查看控制台所能收集到的信息,传入的通知对象是表视图本身:

<UITableView: 0x1eaffe00; frame = (0 0; 320 704); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1e57dfe0>; layer = <CALayer: 0x1e57da50>; contentOffset: {0, 0}>

这显然是调用此函数的问题,因此我将进一步调查,并在收到答案后尽快发布。

现在我将介绍如何调用这个函数:

签收通知:

/**
*  notifies the view controller that its view is about to be added to a view hierarchy
*
*  @param  animated                    whether it will be added in an animated fashion
*/
- (void)viewWillAppear:(BOOL)animated
{
//  add ourselves as an observer to product purchases
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAHelperProductPurchasedNotification object:nil];
}

发送通知:

/**
*  handles all the extra work not that a product has been purchased
*
*  @param  productIdentifier           the purchased product's identifier
*/
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
//  adds the product's id to our purchased product ids list
[_purchasedProductIdentifiers addObject:productIdentifier];
//  stores this purchase in nsuserdefaults for long term use
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
//  save the user defaults
[[NSUserDefaults standardUserDefaults] synchronize];
//  send notification to tohers for awareness of this purchase
[[NSNotificationCenter defaultCenter] postNotificationName:IAHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}

这是一个极其愚蠢的错误:

通知一直在张贴,但不是通过我在问题中包含的方法。

这一切都源于通知名称本身。

我已经在IAPPHelper.h类中实例化了它,如下所示:

//  used to notify listeners when a product is purchased
UIKIT_EXTERN NSString *const IAHelperProductPurchasedNotification;

在实现文件(IAPPHelper.m)中,我一直在做以下操作:

NSString *const IAHelperProductPurchasedNotification;

当我需要这样做的时候:

NSString *const IAHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification";

奇怪的是,通知只是被反复调用,但遗憾的是,很难预测这些愚蠢的bug的行为。

谢谢你们抽出时间来帮助我。

最新更新