Category Archives: swift

Unidirectional data flow in practice

Recently I made a small test project, where I could try out the unidirectional data flow in swift, suggested by Benjamin Encz. You can watch his talk on Realm: https://realm.io/news/benji-encz-unidirectional-data-flow-swift/ What I was interested in, is how can I integrate it into a complex project where I have to perform async tasks. I found out that there is no support for async tasks yet, but you can still achieve it with a workaround. However I would be glad to see it integrated.

The workaround is that in your controller you don’t start actions directly. You implement a service layer instead, and you perform async tasks through that (actually I prefer performing every tasks through the service layer). When the task is done, you create the action inside the service, and send it to the store. So for example if you have to fetch some data from the server, you call the API from the service, and when you get the data you create the corresponding action. The data process part (JSON parsing) can happen either inside the service or the reducer. I’m not sure which option is the better approach.

The only downside is that you have to make sure you return to the main thread. Anyway the profit you gain using this library exceeds this little inconvenience: you have the current state in flows everywhere in the app, and you don’t have to pass it from one controller to another manually. Also you have fresh data everywhere in the app.

Swift: flatMap in action

I am working on an investment related project now. It has plenty of features, I’m not gonna go through them. One of the tasks I had was to show the tickers the user owns in a comma separated list. The problem was the data I had available didn’t contain the necessary field. So I had to iterate through a list of stock objects, and call the appropriate service method to get the ticker for every element. My code looked something like this:

It is working okay, but it is ugly, and hard to understand what it is going on at the first glance. So I started experimenting a bit with flatMap. It turned out to be pretty cool. You can do whatever you like in the closure to every single element of the array, and a new array is returned. Also ensures that there are no nil values in the result. So I ended up with this:

At first it may look strange, but actually it is easy to read, easy to understand.

Swift is awesome!

Functional Programming in Swift

I finally finished reading the book Functional Programming in Swift. I have to say it is a pretty good book. I wouldn’t recommend it for a beginner in Swift, but after you familiarize yourself a bit with the language, it is definitely worth reading.

After this I am seriously considering learning Haskell. I think it would make my functional programming skill much better.

Socket.io in iOS

Recently I read about socket.io. Basically it enables event-based communication between a server and a client. The server side is written in node.js, and they have a lot of client libraries in different languages. The server is open source, under MIT license. You can download it from Github: https://github.com/socketio/socket.io. The concept is really interesting, so I started looking for a solution in iOS.

You can find a few socket.io client libraries out there written in objective-c. But as it turned out, socket.io had released their own client library for iOS. So you should definitely use that. I was more than happy to see, that the library is written is Swift. They also provide objective-c interface. So I fired up Xcode and started a test project. In this article I will show you how to use the library.

Continue reading Socket.io in iOS