Search code examples
swiftrx-swift

Accessing a closure property of an instance in RxSwift


I am trying to deep dive into RxSwift and figure out different approaches. I realized some project in Github using similiar code structure as below. I used to think as imperative way but I really wonder what i am missing in Rxswift world.

    class ViewController: UIViewController {
    
    let observableProperty = PublishSubject<Client.DelegateEvent>()

    struct Client {
        
        static var live: Self { Client(events: { stringInputFromSomeWhere in
            observableProperty.asObservable() // Point 3
        }, setable: {
            { _ = SomeManager().doNothing() }
        }, someId: "cool property")}
        
        
        var events: (String) -> Observable<DelegateEvent>
        var setable: () -> ()
        var someId: String
        
        init(events: @escaping (String) -> Observable<DelegateEvent>,
             setable: @escaping () -> (),
             someId: String
            
        ) {
            self.events = events
            self.setable = setable
            self.someId = someId
        }
    
        public enum DelegateEvent {
            case didUpdate(SpecialLocation)
            case didFail(Error)
        }
        
    }
    
    struct SpecialLocation {
        
    }
    
    class SomeManager {
         func doNothing() {
            print("noThing Worked")
        }
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
        let clearStep = Client.live.someId // Point 1
        let whoAreYou = Client.live // Point 2a 
        whoAreYou.setable() // Point 2b 
        print(whoAreYou.someId)
    
    }

  • Point 1 - Am I accessing a string property of a instance, right ?

  • Point 2ab - I think, I am accessing an instance after calling a closure,Actually what am I doing here?

  • Point 3 - Getting error, Cannot convert value of type '()' to closure result type 'Observable<ViewController.Client.DelegateEvent>' So How can I fix this in a meaningful way ?

Thank you so much for every answer and comment.


Solution

  • Point 1 - Am I accessing a string property of a instance, right ?

    Strictly speaking yes, but the instance will be created just for this access and then immediately destroyed. This is rather silly to do IMO.

    Point 2ab - I think, I am accessing an instance after calling a closure,Actually what am I doing here?

    Point 2a creates a Client value (not the same one used in Point 1, but it will have the same value.)

    Point 2b calls setable() on the value. The value object will be destroyed at the end of the viewDidLoad function.

    Point 3 - Getting error, Cannot convert value of type '()' to closure result type 'Observable<ViewController.Client.DelegateEvent>'

    The Client.live computed property is a global, you are trying to access an instance variable of a particular ViewController from inside that global, but there is no way to define which instance should be accessed.

    So How can I fix this in a meaningful way ?

    This question I cannot answer because I don't know what "fixed" means in this context...