RxSwift into the pit for many days--and finally a little understanding

Source: Internet
Author: User
Tags bind bool emit


First, prefaceThe rivers and lakes are saying that now it is necessary to quickly learn swift, will soon be the world of Swift. In the case of small changes in the API, Swift as a new language, set the director of the family, the general code is really better than OC to use more than the old have heard the concept of MVVM and responsive functional programming, Microsoft is really bad. I've had nothing lately, and I've come into the pit. Ii. Ways of learningRefer to some other people's blog, for the concept of a first understanding, and then refer to the official example, you can start to learn the recommended article:

third, write their own registration login and TableView a little understandingAbout the Observer and the Observer (Observable) emit a sequence UI control under Rxcocoa Some properties are observers (Observable), can emit a sequence, the common control of the text type is controlproperty<string& Gt , the click Tap type that eventually follows the Observabletype protocol button is controlevent<void> and eventually follows the Observabletype protocol

For some of the Bool type properties of the UI control, such as can be entered, clickable, generally with Uibindingobserver<uielementtype, value> (following the Observertype protocol) to generate the observer, Determine whether the accepted data conditions can be entered, clickable

Mark:rx Extended Computed Properties
//TextField can be entered based on the results of the display verification, the validation is over to enter
extension reactive where Base:uitextfield {

 var inp Utenable:uibindingobserver<base, validationresult> {
    return uibindingobserver (uielement:base, binding: { 
        (TextField, result) in

        textfield.isenabled = Result.isvalid
     })
 }
}
About Subject Variable, Publishsubject, which is commonly used in VMS, is one of Subject, which can be used when the observer is BindTo, when the sequence data source Observable Variable It does not terminate properly because of an error, and is suitable for Depending on the source, the Text property that can be used for the control publishsubject differs from the normal subject, and does not immediately trigger a subscription event at the time of subscription, but rather allows us to invoke OnNext (), OnError (), oncompleted to trigger the event at any time, Some common API maps that can be used for button clicks about the observed (Observable) do not produce new sequences flatmaplatest will produce new sequences combinelatest will not produce new sequences about the MVVM folder classification

Before the MVC with the controller in IOS, View one by one, it is well understood, and MVVM controller belongs to V, responsible for processing controllers jump and View and VM bindings, and so on, most of the business logic code is in the VM, it feels like this

The

senses that the model in IOS is very light at all times, and sometimes it's just a modeling class. The feeling should be a variety of data operations, such as database query should be the model of this layer of about * * Two-way binding * * First there are some controls, figure out which property values to listen to these controls, and then build the VM in the corresponding Subject general controller generated VM object, the control of the O The properties of the bservable are bound to the Subject property of the VM, which allows the control property value to be heard in the VM, at which point the Subject is Observer, completes the binding once within the VM, turns Subject into Observable, and generates the corresponding VM Can be observed by attributes (using attributes to save the processed transformed Observable). Here Subject is Observable, can through the map, filter and other operations, the operation of the data is Subject observed sequence, the corresponding module of the entire business logic is here. In the controller, bind the observable attribute of the VM to the UI control, where you can complete the two-way binding

Override Func Viewdidload () {super.viewdidload () Let Regiestviewmodel = Regiestviewmodel ()//bind here: UI Control--& Gt
        VM VMs-UI controls//1.UI controls-VM NameTextField.rx.text.orEmpty. BindTo (Regiestviewmodel.username) . Adddisposableto (Disposebag) pwdTextField.rx.text.orEmpty. BindTo (REGIESTVIEWMODEL.USERPWD). Ad Ddisposableto (Disposebag) repeatPwdTextField.rx.text.orEmpty. BindTo (regiestviewmodel.repeatpwd). addd Isposableto (Disposebag) regiestBtn.rx.tap. BindTo (Regiestviewmodel.registertaps). Adddisposableto (disp Osebag)//2.VM UI Control//Display results on label Regiestviewmodel.usernamevalid. BindTo (NameTipLabel.rx.val Idresult). Adddisposableto (Disposebag)//Bind Password box to enter Regiestviewmodel.usernamevalid. BindTo (PWDT extField.rx.inputEnable). Adddisposableto (Disposebag) regiestviewmodel.passwordvalid. BindTo (Pwdtiplab
 El.rx.validResult)       . Adddisposableto (Disposebag) regiestviewmodel.passwordvalid. BindTo (repeatPwdTextField.rx.inputEnable) 
        . Adddisposableto (Disposebag) regiestviewmodel.repeatpwdvalid. BindTo (RepeatPwdTipLabel.rx.validResult)
        . Adddisposableto (Disposebag)//button is not a binding, the button is subcribe, need to manipulate the regiestviewmodel.registerbuttonenabled . Subscribe (onNext: {[weak self] (result) in self?. regiestbtn.isenabled = result self?. Regiestbtn.alpha = result? 1:0.8}). Adddisposableto (Disposebag)//Registration results: Registration results or failures to be shown on the UI regiestviewmodel.registere Sult. Subscribe (onnext:{[weak self], result in switch result {case let. Failed (Messag e): Self?. Showalter (Message:message) case, OK (message): Self? Showalter (message:message) case empty:self?
Showalter (Message: "")}            }). Adddisposableto (disposebag)//jump to the Login interface button click LoginVcBtn.rx.tap. Subscribe (onNext: { Let LOGINVC = Loginviewcontroller () loginvc.title = "Please log in" Self.navigationcontroller?.
 Pushviewcontroller (LOGINVC, Animated:true)}). Adddisposableto (Disposebag)}
About TableView here need to rxdatasources this supporting framework

The controller needs a dataSource.

Let DataSource = rxtableviewsectionedreloaddatasource<sectionmodel<string, herositem>> ()

The controller uses this dataSource to configure the cell.

Datasource.configurecell = {(_, TableView, Indexpath, item) in
    var cell = Tableview.dequeuereusablecell ( Withidentifier: "Heroscell")
    if cell = = Nil {
        cell = UITableViewCell (style:. Subtitle, Reuseidentifier: " Heroscell ")
    }
    cell!. ImageView?. Image = UIImage (Named:item.icon)
    cell!. Textlabel?. Text = Item.name
    cell!. Detailtextlabel?. Text = Item.intro
    return cell!
}

Create data Observable with VMS, emit sequences, bind to DataSource, complete data binding

Homeviewmode.getsearchresult ()
    . BindTo (TableView.rx.items (Datasource:datasource))
    . Adddisposableto ( Disposebag)
Other operations

TableView's agent

Tableview.rx
. Setdelegate (self)
. Adddisposableto (Disposebag)

TableView Cell's Click

tableView.rx.itemSelected
. map {[weak self] indexpath
    in return (Indexpath, self?. Datasource[indexpath])
}
. Subscribe (onNext: {(Indexpath, item) in
    Self.showalter (Item:item)
})
. Adddisposableto (Disposebag)
It feels like a fixed code pattern that moves the code of the data source into the VM.

In addition, if you do real-time search, with two-way binding effect that is very good, the search box search keyword bound to the VM, in the VM generation sequence bound to TableView on A, preface the river is said now to quickly learn swift, is going to be swift world. In the case of small changes in the API, Swift as a new language, set the director of the family, the general code is really better than OC to use more than the old have heard the concept of MVVM and responsive functional programming, Microsoft is really bad. I have nothing recently, I came to the pit. second, the way of learning to refer to someone else to write some blog, for the concept of a first understanding, and then refer to the official example, you can start to learn the recommended article:
http://www.codertian.com/2016/11/27/RxSwift-ru-keng-ji-read-document/
http://www.codertian.com/2016/12/10/RxSwift-shi-zhan-jie-du-base-demo/ Three, write their own login and tableView a little understanding about the observer and The Observer (Observable) emits a sequence UI control under Rxcocoa Some properties are observers (Observable), can emit a sequence, the common control of the text type is controlproperty<string>, The click Tap type that eventually follows the Observabletype protocol button is controlevent<void> and eventually follows the Observabletype protocol

For some of the Bool type properties of the UI control, such as can be entered, clickable, generally with Uibindingobserver<uielementtype, value> (following the Observertype protocol) to generate the observer, Determine whether the accepted data conditions can be entered, clickable

Mark:rx Extended Computed Properties
//TextField can be entered based on the results of the display verification, the validation is over to enter
extension reactive where Base:uitextfield {

 var inp Utenable:uibindingobserver<base, validationresult> {
    return uibindingobserver (uielement:base, binding: { 
        (TextField, result) in

        textfield.isenabled = Result.isvalid
     })
 }
}
About Subject Variable, Publishsubject, which is commonly used in VMS, is one of Subject, which can be used when the observer is BindTo, when the sequence data source Observable Variable It does not terminate properly because of an error, and is suitable for Depending on the source, the Text property that can be used for the control publishsubject differs from the normal subject, and does not immediately trigger a subscription event at the time of subscription, but rather allows us to invoke OnNext (), OnError (), oncompleted to trigger the event at any time, Some common API maps that can be used for button clicks about the observed (Observable) do not produce new sequences flatmaplatest will produce new sequences combinelatest will not produce new sequences about the MVVM folder classification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.