title: How to replace protocol with DelegateProxy from RxSwift?
body: <p>I'm starting to realize the benefits of using <code>RxSwift</code> and making an attempt to incorporate it into my code, but I'm not sure I'm understanding the usage of <code>DelegateProxy</code>.</p>
  
  <p>Currently in my code I have the following:</p>
  
  <pre><code>struct SampleModel: Decodable
  {
      var first_name: String
      var last_name: String
  
      init(first_name: String, last_name: String)
      {
          self.first_name = first_name
          self.last_name = last_name
      }
  }
  
  class ViewModel
  {
      var model: SampleModel
  
      public var fullName: String {
          return model.first_name + model.last_name
      }
  
      init(model: SampleModel)
      {
          self.model = model
      }
  }
  </code></pre>
  
  <p>I am using a protocol and delegate like so:</p>
  
  <pre><code>protocol NetworkDelegate
  {
      func dataReceived(data: [Decodable]?, error: Error?)
  }
  
  class Network: NSObject
  {
      var databaseLink = String()
  
      var networkDelegate: NetworkDelegate!
  
      init(databaseLink: String)
      {
          super.init()
  
          self.databaseLink = databaseLink
      }
  
      func getDatabaseData&lt;T: Decodable&gt;(model: T.Type)
      {
          guard let url = URL(string: databaseURL) else {
              return
          }
  
          request(url).responseJSON { (json) in
              switch json.result
              {
                  case .success:
                      guard let data = json.data else {
                          return
                      }
  
                      do
                      {
                          let models = try JSONDecoder().decode([T].self,
                                                                from: data)
  
                          self.networkDelegate.dataReceived(data: models,
                                                            error: nil)
                      }
                      catch let jsonErr
                      {
                          print(jsonErr)
                      }
  
                  case .failure(let error):
                      self.networkDelegate.dataReceived(data: nil,
                                                        error: error)
              }
          }
      }
  }
  
  extension ViewController: NetworkDelegate
  {
      func dataReceived(data: [Decodable]?, error: Error?)
      {
          guard let models = data else {
              return
          }
  
          for model in models
          {
              guard let myModel = model as? SampleModel else {
                  return
              }
  
              viewModels.append( ViewModel(model: myModel) )
          }
      }
  }
  
  var viewModels = [ViewModel]()
  weak var networkDelegate: NetworkDelegate?
  
  override func viewDidLoad()
  {
      super.viewDidLoad()
  
      let network = Network(databaseLink: "some url")
      network.networkDelegate = self
      network.getDatabaseData(model: SampleModel.self)
  }
  </code></pre>
  
  <p>On researching this topic <code>RxSwift</code>, I've found the following:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/48316075/is-this-the-best-way-to-convert-swift-protocol-to-rxdelegateproxy">Is this the best way to convert Swift protocol to RxDelegateProxy?</a></li>
  <li><a href="https://stackoverflow.com/questions/41783083/cannot-receive-event-with-custom-delegateproxy-and-protocol">Cannot receive event with custom DelegateProxy and Protocol</a></li>
  </ol>
  
  <p>Using it as reference I created some skeleton code:</p>
  
  <pre><code>import RxSwift
  import RxCocoa
  
  protocol NetworkDelegate: class
  {
      func dataReceived(data: [Decodable]?, error: Error?)
  }
  
  class NetworkDeleteProxy: DelegateProxy&lt;ViewController, NetworkDelegate&gt;, DelegateProxyType
  {
      public static func registerKnownImplementations()
      {
          &lt;#code#&gt;
      }
  
      public static func currentDelegate(for object: ViewController) -&gt; NetworkDelegate?
      {
          return object.networkDelegate
      }
  
      public static func setCurrentDelegate(_ delegate: NetworkDelegate?, to object: ViewController)
      {
          &lt;#code#&gt;
      }
  }
  </code></pre>
  
  <p>However, I'm a bit confused afterwards on how to implement the rest of the functionality and use like the original way:</p>
  
  <pre><code>let network = Network(databaseLink: "some url")
  network.networkDelegate = self
  network.getDatabaseData(model: SampleModel.self)
  </code></pre>
  
  <p>How can I achieve this? Thanks.</p>