2014-07-27 32 views
0

我知道這個問題已被多次詢問,但我仍然遇到問題。我有一些非易耗品的應用程序購買代碼,如果用戶停留在場景中並且一次購買它們,那麼這些代碼工作正常。但是,如果用戶離開此菜單並返回到該位置,代碼將在完成事務時崩潰。我正在使用cocos2d做它的價值。NSNotificationCenter在購買應用程序時發送釋放的消息

MKStoremanager.m

-(void) provideContent: (NSString*) productIdentifier 
{ 
    NSLog(@"productIdentifier=%@", productIdentifier); 
    nc = [NSNotificationCenter defaultCenter]; 


     featureAPurchased = YES; 
     [nc postNotificationName:@"featureAPurchased" object:nil]; //<--crashes here 


    [MKStoreManager updatePurchases]; 
} 

MSStoreObverser.m

- (void) completeTransaction: (SKPaymentTransaction *)transaction {  NSLog(@"completeTransaction"); 
    NSLog(@"transaction.payment.productIdentifier=%@",transaction.payment.productIdentifier); 

    //[[NSNotificationCenter defaultCenter] removeObserver:self]; //<--tried removing observer. Still crashes 

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
    [[MKStoreManager sharedManager] provideContent: transaction.payment.productIdentifier]; 

} 

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
{ 
    NSLog(@"paymentQueue"); 

    for (SKPaymentTransaction *transaction in transactions) 
    { 
     switch (transaction.transactionState) 
     { 
      case SKPaymentTransactionStatePurchased: 

       [self completeTransaction:transaction]; //<--Breaks here 

       NSLog(@"Purchase was a success -J"); 
       break; 

      case SKPaymentTransactionStateFailed: 

       [self failedTransaction:transaction]; 

       break; 

      case SKPaymentTransactionStateRestored: 

       [self restoreTransaction:transaction]; 
       break; 
      default: 

       break; 
     }   
    } 
} 

我認爲這個問題是,NSNotificationCenter觀察者是沒有得到正確刪除,但我不知道爲什麼。

+1

檢查您註冊的代碼以接收此類通知。確保你的代碼也從通知中移除。 – rmaddy

+0

你確定要這樣做嗎? ' - (void)dealloc {[[NSNotificationCenter defaultCenter] removeObserver:self];}' – klcjr89

+0

謝謝rmaddy。這正是我所錯過的。我需要添加[[NSNotificationCenter defaultCenter] removeObserver:self name:@「featureAPurchased」object:nil];到場景代碼。 – PWiggin

回答

2

正如我的評論建議,我們需要刪除觀察者,以確保我們不會發送消息到釋放的東西。

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
相關問題