2016-07-10 20 views
3

我在Swift中創建了一個本機模塊,可以從React Native調用iAPs。 在雨燕模塊我想存儲從JS傳遞給purchaseProduct ,這樣我就可以調用該回調在SKPaymentTransactionObserver委託 但是目標C不喜歡自選的回調,這使得代碼崩潰9次了10React Native:Swift模塊存儲目標C代碼塊用於代理回調

我得到fatal error: unexpectedly found nil while unwrapping an Optional value(@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.NSArray>)

雨燕類:

@objc(StoreManager) 
class StoreManager: NSObject { 
    // callbackholder initialized to an optional as .None with RCTResponseSenderBlock type 
    var purchaseCallbackHolder: RCTResponseSenderBlock? = .None 

    override init() { 
    super.init() 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
    } 

    @objc func purchaseProduct(productIdentifier: String, callback successCallback: RCTResponseSenderBlock) { 
    //omitted purchase code 
    // set purchaseCallbackHolder to callback passed in from JS 
    purchaseCallbackHolder = successCallback 
    } 
} 

extension StoreManager: SKPaymentTransactionObserver { 
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
    for transaction:AnyObject in transactions { 

     if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ 
     switch trans.transactionState { 
     case .Purchased: 
      SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
      break; 
     // omitted extra cases 
     default: 
      break; 
     } 
     } 
     // invoke callback waiting to be called 
     purchaseCallbackHolder?([]) 
     purchaseCallbackHolder = .None 
    } 

    } 
} 

    Objective C block description 
    /** RCTResponseSenderBlock 
    * The type of a block that is capable of sending a response to a bridged 
    * operation. Use this for returning callback methods to JS. 
    */ 
    typedef void (^RCTResponseSenderBlock)(NSArray *response); 

是否有存儲供以後調用obj的C座一個更安全的方法是什麼?

我真的很難過這個,感謝任何幫助!由於

更新 這裏是堆棧跟蹤:

AppName[1788:581487] *** -[StoreManager respondsToSelector:]: message sent to deallocated instance 0x14d43f80 
(lldb) bt 
* thread #4: tid = 0x8df6f, 0x25750ffe CoreFoundation`___forwarding___ + 530, queue = 'com.facebook.react.StoreManagerQueue', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe) 
    * frame #0: 0x25750ffe CoreFoundation`___forwarding___ + 530 
    frame #1: 0x2567b298 CoreFoundation`_CF_forwarding_prep_0 + 24 
    frame #2: 0x2f103f1e StoreKit`__NotifyObserverAboutChanges + 66 
    frame #3: 0x2565bc08 CoreFoundation`CFArrayApplyFunction + 36 
    frame #4: 0x2f103ecc StoreKit`-[SKPaymentQueue _notifyObserversAboutChanges:sendUpdatedDownloads:] + 128 
    frame #5: 0x2f1028e8 StoreKit`-[SKPaymentQueue addPayment:] + 320 
    frame #6: 0x00106438 AppName`StoreManager.purchaseProduct(productIdentifier="iosmonthlysubscription799", successCallback=0x00106694 AppName`partial apply forwarder for reabstraction thunk helper from @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.NSArray>) -> (@unowned()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<Swift.Array<Swift.AnyObject>>) -> (@unowned()) with unmangled suffix ".40" at inAppPayments.swift, self=0x14d0a990) ->()) ->() + 1168 at inAppPayments.swift:54 
    frame #7: 0x00106634 AppName`@objc StoreManager.purchaseProduct(String, callback : ([AnyObject]!) ->()) ->() + 240 at inAppPayments.swift:0 
    frame #8: 0x25752664 CoreFoundation`__invoking___ + 68 
    frame #9: 0x256778bc CoreFoundation`-[NSInvocation invoke] + 292 
    frame #10: 0x2567b356 CoreFoundation`-[NSInvocation invokeWithTarget:] + 50 
    frame #11: 0x001672d6 AppName`-[RCTModuleMethod invokeWithBridge:module:arguments:](self=0x14fdba90, _cmd="invokeWithBridge:module:arguments:", bridge=0x14faa1a0, module=0x14d0a990, [email protected]"2 elements") + 1636 at RCTModuleMethod.m:489 
    frame #12: 0x001abaea AppName`-[RCTBatchedBridge _handleRequestNumber:moduleID:methodID:params:](self=0x14faa1a0, _cmd="_handleRequestNumber:moduleID:methodID:params:", i=6, moduleID=77, methodID=3, [email protected]"2 elements") + 926 at RCTBatchedBridge.m:987 
    frame #13: 0x001aae44 AppName`__33-[RCTBatchedBridge handleBuffer:]_block_invoke.452(.block_descriptor=<unavailable>) + 1028 at RCTBatchedBridge.m:915 
    frame #14: 0x00b9bba6 libdispatch.dylib`_dispatch_call_block_and_release + 10 
    frame #15: 0x00ba64aa libdispatch.dylib`_dispatch_queue_drain + 2014 
    frame #16: 0x00b9ede2 libdispatch.dylib`_dispatch_queue_invoke + 282 
    frame #17: 0x00ba799e libdispatch.dylib`_dispatch_root_queue_drain + 426 
    frame #18: 0x00ba77f0 libdispatch.dylib`_dispatch_worker_thread3 + 100 
    frame #19: 0x2547ee0c libsystem_pthread.dylib`_pthread_wqthread + 1024 
    frame #20: 0x2547e9fc libsystem_pthread.dylib`start_wqthread + 8 

回答

0

交易觀察者需要在DEINIT方法去除:

override init() { 
    super.init() 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
    } 


    deinit { 
    if SKPaymentQueue.canMakePayments() { 
     SKPaymentQueue.defaultQueue().removeTransactionObserver(self) 
    } 
    } 

我擡頭一看,我在得到錯誤我的堆棧跟蹤和其中一個:

StoreKit__NotifyObserverAboutChanges 

指給我這篇文章: A single bug in my StoreKit code that lost me 90% of IAP sales