Search code examples
swiftphotomultipeer-connectivity

Sending data between 2 classes with Delegate Methods


I am trying to send data between 2 classes via MultipeerConnectivity. I am trying to do this via delegate methods.

I have defined this protocol:

protocol AssetDelegate {
    func changeThisName(data: Data)
}

My main ViewController is the AssetDelegate and has implemented the function simply as:

func changeThisName(data: Data) {
    print("changeThisName called")
    if let image = UIImage(data: data) {
        imageViewAsset.image = image
    }
}

This is simply to see if a received image can be decoded and assigned to the imageViewAsset UI element.

This method is called within a MultipeerConnectivity "Manager" class that I have defined, which has an instance in the AppDelegate (is this the definition of a "singleton"?)

My "MPCManager" class has the various MCSession delegate methods, specifically session(didReceive:data) method, which is:

var assetDelegate: AssetDelegate?

func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
    print("Data Recieved: \(data)")
    self.assetDelegate?.changeThisName(data: data)
}

I see the print("Data Received:(data)") in the console log. But I do not see the print statement of the changeThisName method.

This is the console log output. Essentially there are 2 rows showing the MPC connectivity ("other state" and "session connected"). Then there are 2 "Received Data" items.

2019-08-14 12:42:29.714402+0100 My App[34860:11895498] [Accessibility] ****************** Loading GAX Client Bundle ****************
2019-08-14 12:42:44.121481+0100 My App[34860:11895563]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
other state...<MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:45.493567+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.542647+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.586389+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.641138+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.730210+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.795905+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.968786+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
session connected... <MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:46.037399+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.379153+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.444389+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.099950+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.158868+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.351072+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.390215+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.856339+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.922462+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 8269 bytes
2019-08-14 12:42:51.375239+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:51.454734+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.913337+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.977319+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 4569624 bytes
2019-08-14 12:42:54.271102+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [0].
2019-08-14 12:42:54.293615+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [1].
2019-08-14 12:42:54.306191+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.306387+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [2].
2019-08-14 12:42:54.313382+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.313444+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [3].
2019-08-14 12:42:54.318396+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.318453+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [5].
2019-08-14 12:42:54.323359+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.323407+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [6].
2019-08-14 12:42:54.328377+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)

Solution

  • protocol AssetDelegate {
        func changeThisName(data: Data)
    }
    
    class A: AssetDelegate {
        func goToClassB(){
            var b = B()
            b.assetDelegate = self // <-- I don't see this line in your code
        }
    }
    
    class B {
        var assetDelegate: AssetDelegate?
    
        func passDataToA(){
            self.assetDelegate?.changeThisName(whatEverDataHere)
        }
    }