2012-10-17 44 views
0

这里就是我称之为:enumerateObjectsUsingBlock:未执行

/** 
* 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; 
     } 
    }]; 
} 

然而,该块永远不会被调用,因此表格视图单元格是永远不会被重新加载。我可以告诉块永远不会执行,因为日志永远不会显示在控制台中。但是,通过放置一个断点,我可以确定productPurchased:方法肯定会被调用。

任何帮助表示赞赏。

谢谢。

编辑:我现在已经改变了上面的代码,以允许在执行过程中有更多的日志记录。从我已经能够通过查看控制台收集,在传递的通知对象是表视图本身:

<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]; 
} 
+3

您是否检查_products是否为空或空? – Eiko

+0

你是否在块内部放置了'NSLog'语句,但是在'if'语句之外。 – mttrb

+0

这似乎是一个higg错误的儿子。做一个测试,并尝试在方法调用开始时获得nslog的输出。如果你没有得到它,那么Xcode编译你的应用程序,但不要安装它,你会得到你旧版本的旧版痕迹,而gdb会工作,但我只是猜测。 –

回答

0

这是一个令人难以置信愚蠢的错误:

通知正在不断发布,但不是通过我在问题中包含的方法。

这一切都来自通知名称本身。

,我已实例化它在IAPHelper.h类,像这样:

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

,并在实现文件(IAPHelper.m)我一直在做这样的:

NSString *const IAHelperProductPurchasedNotification; 

当我需要这样做:

NSString *const IAHelperProductPurchasedNotification = @"IAPHelperProductPurchasedNotification"; 

很奇怪,通知只是被反复调用,bu唉,很难预测这些愚蠢的错误的行为。

感谢您花时间帮助我们。