1

I have integrated the Pusher framework for my application in Swift 3 using cocoa pods [ pod 'PusherSwift' ].

These are the lines of code :

let pusher = Pusher(key: "XXXXXXXXXXXXXXXXXXXX")
// subscribe to channel and bind to event
let channel = pusher.subscribe("test_channel")
let _ = channel.bind(eventName: "my_event", callback: { (data: Any?) -> Void in
    if let data = data as? [String : AnyObject] {
        if let message = data["message"] as? String {
            print(message)
        }
    }
})
pusher.connect() 

The app crashes at pusher.connect() at the line - self.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable"). No crash report is shown.

open lazy var reachability: Reachability? = {
        let reachability = Reachability.init()
        reachability?.whenReachable = { [unowned self] reachability in
            self.delegate?.debugLog?(message: "[PUSHER DEBUG] Network reachable")
            if self.connectionState == .disconnected || self.connectionState == .reconnectingWhenNetworkBecomesReachable {
                self.attemptReconnect()
            }
        }
        reachability?.whenUnreachable = { [unowned self] reachability in
            self.delegate?.debugLog?(message: "[PUSHER DEBUG] Network unreachable")
        }
        return reachability
    }()
Nic
  • 12,220
  • 20
  • 77
  • 105

2 Answers2

3

This looks like you might be getting bitten by the same issue described here.

I think it's that the PusherConnection object is taken as unowned into the reachability closure but because you're not keeping a reference to the Pusher instance outside of the viewDidLoad function then the connection object gets cleaned up whereas the reachability object does not.

So, to fix this you probably need to declare the pusher object outside of the function where you instantiate it, so that it hangs around. e.g.

class ViewController: UIViewController, PusherDelegate {
    var pusher: Pusher! = nil
    ...

and then within viewDidLoad do pusher = Pusher(... as normal.

0

I don't think you need to use pusher.connect().

See for example detailed docs:

let pusher = Pusher(key: "YOUR_APP_KEY")
let myChannel = pusher.subscribe("my-channel")

myChannel.bind(eventName: "new-price", callback: { (data: Any?) -> Void in
    if let data = data as? [String : AnyObject] {
        if let price = data["price"] as? String, company = data["company"] as? String {
            print("\(company) is now priced at \(price)")
        }
    }
})

Alternatively try this first and see if it connects:

let pusher = Pusher(key: "XXXXXXXXXXXXXXXXXXXX")
pusher.connect() 

Then bind to your channel.

Nic
  • 12,220
  • 20
  • 77
  • 105