O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reactive Programming with Swift 4

Book Description

Learn how to solve blocking user experience and build event based reactive applications with Swift.

About This Book

  • Build fast and scalable apps with RxSwift
  • Apply reactive programming to solve complex problems and build efficient programs with reactive user interfaces
  • Take expressiveness, scalability, and maintainability of your Swift code to the next level with this practical guide

Who This Book Is For

This book is for the developers who are familiar with Swift and iOS application development and are looking out to reduce the complexity of their apps. Prior experience of reactive programming is not necessary.

What You Will Learn

  • Understand the practical benefits of Rx on a mobile platform
  • Explore the building blocks of Rx, and Rx data flows with marble diagrams
  • Learn how to convert an existing code base into RxSwift code base
  • Learn how to debug and test your Rx Code
  • Work with Playgrounds to transform sequences by filtering them using map, flatmap and other operators
  • Learn how to combine different operators to work with Events in a more controlled manner.
  • Discover RxCocoa and convert your simple UI elements to Reactive components
  • Build a complete RxSwift app using MVVM as design pattern

In Detail

RxSwift belongs to a large family of Rx implementations in different programming languages that share almost identical syntax and semantics. Reactive approach will help you to write clean, cohesive, resilient, scalable, and maintainable code with highly configurable behavior.

This book will introduce you to the world of reactive programming, primarily focusing on mobile platforms. It will tell how you can benefit from using RxSwift in your projects, existing or new. Further on, the book will demonstrate the unbelievable ease of configuring asynchronous behavior and other aspects of the app that are traditionally considered to be hard to implement and maintain. It will explain what Rx is made of, and how to switch to reactive way of thinking to get the most out of it. Also, test production code using RxTest and the red/ green approach. Finally, the book will dive into real-world recipes and show you how to build a real-world app by applying the reactive paradigm.

By the end of the book, you'll be able to build a reactive swift application by leveraging all the concepts this book takes you through.

Style and approach

This book is a definite tutorial in FRP with Swift filled with well-described examples.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Reactive Programming with Swift 4
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Migrating from Swift 3 to Swift 4
    1. What's new in Swift 4?
      1. Setting up the environment
      2. What’s changed?
        1. Changes/improvements in Dictionary
          1. Grouping initializer
          2. Key-based subscript with default value
          3. Convert tuples to Dictionary
          4. Convert arrays to Dictionary
          5. Resolving duplicates
          6. Reserving capacity
        2. Changes/improvements in Strings
          1. Bid bye to string.characters
          2. String protocol
          3. Changed interpretation of grapheme clusters
        3. Access modifiers
        4. What's new
          1. JSON encoding and decoding
          2. Multiline String literals
          3. Smart key paths
          4. One sided ranges
          5. Pattern matching with one sided ranges.
          6. swap versus swapAt
          7. Improved NSNumber
          8. Directly access unicode scalars of characters
      3. Migrating to Swift 4
        1. Preparation before migration
        2. Swift migration assistant
        3. Swift 4 migration changes overview
        4. SDK changes
        5. Notable special cases
          1. New String
          2. Differentiating between single-tuple and multiple-argument function types
          3. Adding tuple destructuring
          4. Default parameter values must be public
        6. After migration
        7. Known migration issues
        8. Using Carthage/CocoaPods projects
    2. Summary
  7. FRP Fundamentals, Terminology, and Basic Building Blocks
    1. Functional reactive programming
      1. What is functional reactive programming (FRP)?
        1. Imperative programming
        2. Functional programming
        3. Reactive programming
      2. Basic building blocks
        1. Event streams
        2. State
        3. Side effects
        4. Immutability
      3. RxSwift foundation
        1. Reactive extensions
        2. Observable
        3. Subject
        4. Marble diagrams
        5. Schedulers
      4. Railway-oriented programming
    2. Summary
  8. Set up RxSwift and Convert a Basic Login App to its RxSwift Counterpart
    1. RxSwift in action
      1. Let's build the environment first
      2. Setting up the environment
        1. RxSwift with CocoaPods
        2. Installing RxSwift
        3. Installing RxSwift with Carthage
      3. Let's get started – The Login page
        1. Setting up
          1. Steps to set up
        2. Functional programming to the rescue
    2. Summary
  9. When to Become Reactive?
    1. Creating and subscribing to Observables
      1. Project setup
      2. Getting started
      3. Different types of subject
        1. PublishSubject in action
        2. BehaviorSubject in action
        3. ReplaySubject in action
        4. Variable in action
    2. Understanding traits and its types
      1. What are traits?
        1. Different types of trait
          1. The single trait
          2. The completable trait
          3. The maybe trait
    3. Summary
  10. Filter, Transform, and Simplify
    1. Working with events using operators
      1. Environment setup
      2. Transforming operators
        1. flatMap and flatMapLatest in action
        2. Scan, reduce, and buffer
          1. Scan
          2. Buffer
    2. Filtering operators
      1. The filter operator
        1. The distinctUntilChanged operator
        2. The takeWhile operator
    3. Summary
  11. Reduce by Combining and Filtering and Common Trade Offs
    1. Combining and filtering Observable sequences
      1. Project setup
    2. Concatenating and prefixing
      1. Merging
      2. Combining elements of different types
        1. Introducing zip
      3. Performing side effects
        1. doOn in action
          1. Setting the project
          2. Building the app
    3. Summary
  12. React to UI Events – Start Subscribing
    1. RxCocoa traits
      1. Types of RxCocoa traits
        1. Driver
        2. ControlProperty
        3. ControlEvent
    2. Binding UI elements in practice
      1. Binding data to UI
      2. Binding UITableViews
      3. Configuring cell rows in a TableView
        1. Selecting a row in TableView
      4. Binding UICollectionViews
    3. Summary
  13. RxTest and Custom Rx Extensions – Testing with Rx
    1. Testing in RxSwift
      1. RxTest
        1. Testing in practice with HotObservables
        2. Testing in RxTest with ColdObservables
      2. RxBlocking
        1. Testing with RxBlocking
    2. Summary
  14. Testing Your RxCode – Testing Asynchronous Code
    1. Tracking down issues – Debugging Rx code
      1. Debugging with debug
      2. Debugging with total
      3. Testing asynchronous code
    2. Summary
  15. Schedule Your Tasks, Don't Queue!
    1. Queues and schedulers
      1. Scheduler Singletons
        1. Specifying a scheduler
      2. Schedulers in practice
        1. ConcurrentDispatchQueueScheduler
        2. SerialDispatchQueueScheduler
        3. OperationQueueScheduler
    2. Summary
  16. Subscribe to Errors and Save Your App
    1. Error handling
      1. The catchErrorJustReturn operator
      2. The catchError operator
      3. The retry operator
      4. onErrorJustReturn
      5. onErrorDriveWith
      6. onErrorRecover
    2. Summary
  17. Functional and Reactive App-Architecture
    1. Design patterns
      1. Singleton design pattern
        1. Implementation
          1. Singleton design pattern – Pros and cons
      2. Key value coding (KVC)
        1. KVC – Implementation
      3. Notifications
        1. Notifications – Implementation
      4. Model view controller – MVC
        1. The model
        2. The view
        3. The controller
        4. MVC implementation
    2. Summary
  18. Finish a Real-World Application
    1. Networking in RxSwift
      1. Project setup
      2. Project implementation
        1. Fetching and parsing data
        2. Binding fetched data to View elements
        3. Build and run
      3. Some other cool libraries
    2. Summary
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think