Skip to main content

RxSwift qua các ví dụ thưc tế - zip


1. Lời dẫn: RxSwift là gì?

Mình định nghĩa một cách đơn giản
Là một thư viện giúp việc lập trình bất đồng bộ trở nên dễ dàng hơn.
Dễ dàng hơn như thế nào mình sẽ cùng các bạn đi qua các vị dụ. Các bạn chỉ cần nắm vững các phép toán cơ bản sau là có thể làm việc thoải mái với RxSwift rồi
  • zip
  • map
  • flatMap
  • filter
  • merge
  • retry
  • kết hợp các phép toán

2. Ví dụ thực tế - phép toán zip

Bài toán: Bạn phải validate một image có
  • chiều rộng > x
  • chiều dài < y
  • file size < z
Tuy nhiên các giá trị trên phải lấy từ phía server.
Giả sử hàm lấy thông số như sau:
func getSetting(name: String, onCompleted: (String) -> Void)
Cách làm đơn giản nhất là dùng các callback lồng nhau như sau:
getSetting(name: "minWidth") { minWidth in
    getSetting(name: "minHeight") { minHeight in
        getSetting(name: "maxSize") { maxSize in
            // use minWidth, minHeight, maxSize here
        }
    }
}
Các bạn dễ dàng nhận thấy những dòng code phía trên là những dòng code tồi. Càng nhiều giá trị cần lấy, logic càng phức tạp khiến việc bảo trì code gặp khó khăn.

Tiếp theo ta cùng xem phiên bản sử dụng RxSwift
Observable.zip(
    getSettingObservable(name: "minWidth"),
    getSettingObservable(name: "minHeight"),
    getSettingObservable(name: "maxSize")
).subscribe(onNext: minWidth, minHeight, maxSize in
    // use minWidth, minHeight, maxSize here
)
Có vẻ có nhiều từ mới xuất hiện, nhưng những dòng code đã có vẻ "sạch" hơn, và ta cũng có thể đoán được nó làm việc gì: nhóm việc lấy các thông số minWidth, minHeight, maxSize lại, sau đó sử dụng thì tất cả các giá trị đó sẵn sàng.

Mình xin giải thích các từ mới xuất hiện, trước hết là:
func getSettingObservable(name: String) -> Observable<String>
RxSwift sử dụng dữ liệu kiểu Observable. Để đơn giản các bạn có thể hiểu thay vì sử dụng giá trị thông qua callback như sau
getSetting(name: "settingName") { settingValue in
    // use settingValue
}
các bạn có thể sử dụng thông qua Observable như sau
getSettingObservable(name: "settingName")
    .subcribe(onNext: { settingValue in 
        // use settingValue here
    }
)
Không thay đổi quá nhiều đúng không nào.

Implement của function getSettingObservable
func getSettingObservable(name: String) -> Observable<String> {
    return Observable.create { observer in
        getSetting(name: name) { value in
            observer.onNext(value)
            observer.onComplete()
        }
        return Disposables.create()
    }
}

Sau khi các bạn đã chuyển callback bình thường thành dữ liệu kiểu Observable các bạn có thể sử dụng phép toán zip của RxSwift để sử dụng các giá trị sau khi tất cả đã sãn sàng.

3. Kết

Đọc đến đây có lẽ các bạn sẽ có chung một cảm giác là để thực hiện một công việc đơn giản thế tại sao phải dài dòng thế làm gì.
Tuy nhiên RxSwift sẽ phát huy sức mạnh trước những bài toán bất đồng bộ phức tạp hơn mà chúng ta sẽ dần khám phá qua các bài sau của series.
Chân thành cám ơn các bạn đã theo dõi!

Comments

Popular posts from this blog

Swift Tool Belt, Part 1: Adding a Border, Corner Radius, and Shadow to a UIView with Interface Builder

During my iOS work, I’ve assembled a set of code that I bring with me on every iOS project. I’m not talking about large frameworks or CocoaPods here. These are smaller Swift extensions or control overrides that are applicable to many projects. I think of them as my tool belt. In this post, I’ll show you an extension that will add a border, a corner radius, and a shadow to any UIView, UIButton, or UILabel and allow you to preview what it will look like in Interface Builder. Back in 2014, I wrote a blog post on Expanding User-Defined Runtime Attributes in Xcode where I added a border, corner radius, and shadow to a UIView using Interface Builder’s user-defined runtime attributes. This solution had no type checking—you had to type the property you wanted to modify by hand and often had to look up what it was called. You also had to run your project in order to see the effect of the runtime attribute. Starting with Xcode 6 , there is a new mech...

Alamofire vs URLSession

Alamofire vs URLSession: a comparison for networking in Swift Alamofire and URLSession both help you to make network requests in Swift. The URLSession API is part of the foundation framework, whereas Alamofire needs to be added as an external dependency. Many  developers  doubt  whether it’s needed to include an extra dependency on something basic like networking in Swift. In the end, it’s perfectly doable to implement a networking layer with the great URLSession API’s which are available nowadays. This blog post is here to compare both frameworks and to find out when to add Alamofire as an external dependency. Build better iOS apps faster Looking for a great mobile CI/CD solution that has tons of iOS-specific tools, smooth code signing, and even real device testing? Learn more about Bitrise’s iOS specific solutions! This shows the real power of Alamofire as the framework makes a lot of things easier. What is Alamofire? Where URLSession...

Frame vs Bounds in iOS

This article is a repost of an answer I wrote on Stack Overflow . Short description frame = a view’s location and size using the parent view’s coordinate system ( important for placing the view in the parent) bounds = a view’s location and size using its own coordinate system (important for placing the view’s content or subviews within itself) Details To help me remember frame , I think of a picture frame on a wall . The picture frame is like the border of a view. I can hang the picture anywhere I want on the wall. In the same way, I can put a view anywhere I want inside a parent view (also called a superview). The parent view is like the wall. The origin of the coordinate system in iOS is the top left. We can put our view at the origin of the superview by setting the view frame’s x-y coordinates to (0, 0), which is like hanging our picture in the very top left corner of the wall. To move it right, increase x, to move it down increase y. To help me remember bound...