2 Replies
      Latest reply: Oct 24, 2016 1:48 PM by jordanz RSS
      inamit Level 1 Level 1 (0 points)

        hello,

        I'm trying to work with PubNub, when I "installed" the SDK and run the project, it was written "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_l386_INPOV, subcode=0x0)"

        that's my ViewController.swift:

        import UIKit
        import PubNub
        class UpdatesViewController: UIViewController {
            var client: PubNub!
            override func viewDidLoad() {
                super.viewDidLoad()
                /
                let configuration = PNConfiguration(publishKey: "demo", subscribeKey: "demo")
                /*
                 Subscription process results arrive to listener which should adopt to PNObjectEventListener protocol
                 and registered using:
                 */
                self.client.addListener(self as! PNObjectEventListener)
                self.client.subscribeToChannels(["my_channel1"], withPresence: false)
              
              
                /
                func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
                  
                    /
                    if message.data.channel != message.data.subscription {
                      
                        /
                    }
                    else {
                      
                        /
                    }
                  
                    print("Received message: \(message.data.message) on channel \(message.data.channel) " +
                        "at \(message.data.timetoken)")
                }
              
                /
                func client(_ client: PubNub, didReceive status: PNStatus) {
                  
                    if status.operation == .subscribeOperation {
                      
                        /
                        if status.category == .PNConnectedCategory || status.category == .PNReconnectedCategory {
                          
                            let subscribeStatus: PNSubscribeStatus = status as! PNSubscribeStatus
                            if subscribeStatus.category == .PNConnectedCategory {
                              
                                /
                            }
                            else {
                              
                                /*
                                 This usually occurs if subscribe temporarily fails but reconnects. This means there was
                                 an error but there is no longer any issue.
                                 */
                            }
                        }
                        else if status.category == .PNUnexpectedDisconnectCategory {
                          
                            /*
                             This is usually an issue with the internet connection, this is an error, handle
                             appropriately retry will be called automatically.
                             */
                        }
                            /
                            /
                        else {
                          
                            let errorStatus: PNErrorStatus = status as! PNErrorStatus
                            if errorStatus.category == .PNAccessDeniedCategory {
                              
                                /*
                                 This means that PAM does allow this client to subscribe to this channel and channel group
                                 configuration. This is another explicit error.
                                 */
                            }
                            else {
                              
                                /*
                                 More errors can be directly specified by creating explicit cases for other error categories
                                 of `PNStatusCategory` such as: `PNDecryptionErrorCategory`,
                                 `PNMalformedFilterExpressionCategory`, `PNMalformedResponseCategory`, `PNTimeoutCategory`
                                 or `PNNetworkIssuesCategory`
                                 */
                            }
                        }
                    }
                    else if status.operation == .unsubscribeOperation {
                      
                        if status.category == .PNDisconnectedCategory {
                          
                            /*
                             This is the expected category for an unsubscribe. This means there was no error in
                             unsubscribing from everything.
                             */
                        }
                    }
                    else if status.operation == .heartbeatOperation {
                      
                        /*
                         Heartbeat operations can in fact have errors, so it is important to check first for an error.
                         For more information on how to configure heartbeat notifications through the status
                         PNObjectEventListener callback, consult                  */
                        
                        if !status.isError { / Heartbeat operation was successful. */ }
                        else { / There was an error with the heartbeat operation, handle here. */ }
                    }
                }
            }
            
            override func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
                / 
            }
            
            /
            /  
            / 
            override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                / 
                / 
            }
            */
        }
        

        that's my AppDelegate.swift(normal, entered just var client: PubNub!:

        import UIKit
        import PubNub
        @UIApplicationMain
        class AppDelegate: UIResponder, UIApplicationDelegate, PNObjectEventListener {
           
            var window: UIWindow?
           
            var client: PubNub!
           
            func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
                /
                return true
            }
            func applicationWillResignActive(_ application: UIApplication) {
                /
                /
            }
            func applicationDidEnterBackground(_ application: UIApplication) {
                /
                /
            }
            func applicationWillEnterForeground(_ application: UIApplication) {
                /
            }
            func applicationDidBecomeActive(_ application: UIApplication) {
                /
            }
            func applicationWillTerminate(_ application: UIApplication) {
                /
            }
           
        }
         

         

        how can I fix it? thanks for all the answers

        • Re: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_l386_INPOV, subcode=0x0)
          eskimo Apple Staff Apple Staff (7,505 points)

          I don’t know anything about the PubNub SDK but the following is worrying:

          var client: PubNub!
          

          In both your app delegate and your view controller you declare this value (presumably your connection to the SDK) as an implicitly unwrapped optional and then access it without ever initialising it to some value.  That means the value will be nil and Swift will trap as you’re seeing.  You have to initialise this at some point.  How you do that depends on the details of the PubNub SDK.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_l386_INPOV, subcode=0x0)
              jordanz Level 1 Level 1 (0 points)

              Hi!

               

              I stumbled upon this and there is definitely an issue in the code you wrote. You never create (initialize) a PubNub instance before trying to use it.

               

              You need something like this after line 8 in ViewController.swift

               

              ```

              client = PubNub.clientWithConfiguration(configuration)

              ```

               

              This needs to occur before you perform any actions on your forcibly unwrapped instance variable.