The beginning of the nonsense: The RxSwift Driver is usually used to bind UI operations that need to be performed on the main thread. The most common one is to bind the DataSource of UITableView. It takes a lot of patience to understand, so in order to deepen the understanding of the RxSwift Driver, package a simple OC version.

Links to previous articles

IOS encapsulates a simple UITableView chain listening click events function ideas and implementation

Dispose and DisposeBag under click events

First, achieve the effect preview

The implementation code

Implementation effect

If the code is similar to RXSwift, let’s look at how RXSwift defines the drive method of a Driver.

Explain:

Declare a drive method that takes two arguments and returns R2. The first parameter is a method, which is an Observable sequence of parameters of type Observable. The return value is a closure of R1 and R2. The second parameter is R1.

Isn’t that a little convoluted? ! ?

So, implement a KDSDriver class along these lines

Maybe there’s a little bit of a question here, why don’t WE explain the implementation of the Driver class in RXSwift, so you can understand that ultimately what we’re going to implement is the internal sinking of the block that returns the UITableViewCell, Upload to the real UITableViewDataSource when the external condition (onNext) triggers a UI refresh.

Second,KDSDriverClass to encapsulateKDSDriver.h

The complete code

You can see that drive and onNext are implemented internally,

1.drivemethods

The method still takes the form of a return block, which returns a KDSDriveBlock,

KDSDriveBlock needs to return WSLDispose object, which is used to destroy temporary objects created internally in the process of tedious code sinking. Parameter obj is the drive object to bind (TableView), Sel this method is the obj (tableview) need to be performed, the method of driverTableViewDataSourceBlock is a KDSDriverTableViewDataSourceBlock type of block, the purpose is very simple, That’s a block that holds a UITableViewCell that can be returned on demand.

2,KDSDriverTableViewDataSourceBlock

T is the generic type when the KDSDriver object is declared

The statement

create

Three,KDSDriverClass to encapsulateKDSDriver.m

Four, encapsulation rxItems under the UITableView classification: the driverTableViewDataSourceBlock method

In order to satisfy the requirement of the KDSDriver object dirve method return value of the KDSDriveBlock parameter, we need to create a SEL (method) under the UITableView category.

UITableView (KDS).h

Parameter interpretation

KDSDriver: Data-driven object

KDSDriverTableViewDataSourceBlock: UITableViewCell receipt block

Return value interpretation

KDSDriveBlockResultBlock:

The significance of this type of Block is binding UITableViewCell receipt KDSDriverTableViewDataSourceBlock to real TableViewDataSource objects, When outside KDSDriver send onNext event TableViewDataSource in executing agency method tableView: cellForRowAtIndexPath: Return this KDSDriverTableViewDataSourceBlock run results, and returns a WSLDispose object (used for destruction operation)

UITableView (KDS).m

1 in the red circle:

Set the UITableView’s proxy

2 in the red circle:

Dispose UITableView associated object dispose;

Bound to perform under the drive onNext events to refresh KDSDriverDataSourceRefreshBlock KDSTableViewDataSourceProxy proxy objects inside, The operation of trigger KDSTableViewDataSourceProxy refresh UITableView.

Dispose object of temporary variables is held temporarily to prevent early release.

Five, the encapsulationKDSTableViewDataSourceProxy

KDSTableViewDataSourceProxy.h

KDSTableViewDataSourceProxy.m

6. Operation results

Code implementation

Cell Click event

Data source driver returncell

Click on the normal

See if variables created temporarily inside the KDSDriver data operation and stored in the KDSDisposeBag can be destroyed properly when the current UIViewController is destroyed.

As you can see, all temporary variables created internally are destroyed.

Seven, summary and thinking

It is the idea of this paper to define the code part that external programmers need to deal with flexibly in the form of blocks, and then transfer it to the internal agent that can be operated, and trigger it at a specific time. In fact, RxSwift is also implemented in this way. So don’t get confused about why writing a lot of code is a one-sentence thing

Article is only for personal thinking, poor code, god do not laugh