(dotSwift) Unidirectional data flow with RxSwift and RxRealm

In my talk at dotSwift 2017 I start with generic overview of some of the RxSwift basics and move to three complete code examples. In three posts I post the sample code and comment shortly why I chose to highlight these exact examples. I already posted a write up on the GitHub API search example here: http://rx-marin.com/post/dotswift-search-github-json-api/. The second post in the series, the one about presenting view controllers, is online here: http://rx-marin.com/post/dotswift-rxswift-view-controller/.

(dotSwift) Presenting View Controllers with RxSwift

In my talk at dotSwift 2017 I start with generic overview of some of the RxSwift basics and move to three complete code examples. In three posts I’ll post the sample code and comment shortly why I chose to highlight these exact examples. I already posted a write up on the GitHub API search example here: http://rx-marin.com/post/dotswift-search-github-json-api/. Let’s continue with example number two. Presenting a View Controller from RxSwift RxSwift doesn’t neccessarily force you into one architecture or another, it’s really up to you to chose how are you going to structure your application.

(dotSwift) Search GitHub with RxSwift

In my talk at dotSwift 2017 I start with generic overview of some of the RxSwift basics and move to three complete code examples. In three posts I’ll post the sample code and comment shortly why I chose to highlight these exact examples. Let’s start with number one. Searching for GitHub repositories with RxSwift The first example in the talk is a simple single-screen application that allows the user to enter a repository name (or part of it) and search GitHub for matching results.

A smarter Retry with RxSwiftExt

Sometimes your observable will fail with an error but that would not mean that you need to give up trying. For example saving to a file might fail because the file is locked temporarily but a split second later would be unlocked and ready for your changes. Same goes for web requests - there might be a temporary glitch in connectivity which could make the observable fail. RxSwift offers a special operator called retry, which allows you to try another time the observable subscription upon error.

Implementing state with scan in RxSwift

Intro Common misconception is that you cannot have state with Rx. Well you can - and there’s a special operator that helps you to: scan. If you’ve ever used reduce in Swift - scan is a very similar operator but reduce goes over the complete sequence and gives you the final value of the accumulator while scan emits each intermediate value as well. If you haven’t used reduce - no worries you’ll get to understand scan from the examples below.

Custom bindings with RxSwift

Intro After I did some convenience operators of my own, which really made things easier while writing code I thought I’d be a good idea to look into building more stuff on my own. It helps learning and it’s healthy :) Binding to make a view visible I noticed there is a binding on the hidden property of UIView but sometimes the code just reads better when you don’t have to think of hiding something but rather showing something.

Custom convenience operators with RxSwift, Part 2

Intro I got some great feedback on last week’s post on convenience operators part 1 so I’m really excited to publish part 2, which I hope will be even more interesting for those of you who are looking into RxSwift. Without further ado let’s dive in code… A better negate() operator First of all I have a better version of my negate() operator from last week for you. What I wrote on my own was a pretty simple function that looked like so: extension Observable where Element: BooleanType { public func negate() -> Observable<Bool> { return map {value in !value} } } It doesn’t get simpler than that, right?

Custom convenience operators with RxSwift, Part 1

Intro Just like when learning a new language you need to build up a dictionary to start understanding how the language works, you got to learn the Rx operators and everything will eventually click together. And then once you have a good common of a language it’s okay to start coming up with new words too to boost the expressiveness of your speech and for convenience. Same thing happened with me last week - for the first time I felt like I’m getting work done with RxSwift because I had good understanding how to use at least few operators.

Split laps timer with RxSwift and RxCocoa: Part 2

In my post from last week I worked on creating a split lapse timer app (last week’s post). But later on when I was playing with the application I noticed that I naturally would like to have means to start or stop the timer.

Well this week I am implementing exactly this functionality.

The first thing I thought about was how to implement state in my app because a timer clearly has two distinct states either running or not running. That got me thinking about combining signals, mapping, you know, all the good stuff.

If you want to follow along you can download the starter project I prepared. It is in the shape where last weeks blog posts leaves off but I’ve added a couple of buttons in the user interface:

Download the starter project to follow along here: rx_laptimer_starter.zip

Now let’s put all those buttons to work!

Split laps timer with RxSwift and RxCocoa

I was browsing through RxMarbles and was totally baffled by the sample function. The marble diagram looks pretty random at first sight:

At first I thought - “Hey, that second sequence is getting totally ignored!”. But after I read the description I figured it out:

The first sequence’s elements is what sample emits, while the second sequence’s elements determine when sample emits. So in a way yes - the actual values A, B, C, D do get totally ignored.

When it was clear to me what sample does I started wondering if this function has any practical application :]

This brought me to creating a split lap timer app to test what sample can do for me. In the finished project I have a timer emitting time values (aka the first sequence) and I want to grab (or sample) the values whenever the user taps a button (aka the second sequence).

Here’s how the marble diagram looks like for the app setup:

And this is how the app looks like when finished:

Let’s build that app :]