SwiftUI, Combine, OLXAleksander LorencBlockedUnblockFollowFollowingJun 13WWDC, Preparations, ExcitementThe dust after this year’s WWDC has not yet settled, and we’re still discovering many things that Apple introduced to us — Sign-In with Apple, Combine, SwiftUI, Catalina, Catalyst… The list could literally cover the whole screen.
What got me, and probably every other iOS developer excited the most is, of course, SwiftUI — the declarative framework for creating views using code.
Surprisingly, just a week before it was released, I gave a talk about creating views in code for our mobile team.
While I saw many advantages in creating views with code, I couldn’t ignore its flaws — lots of code to set up constraints (I prefer not to use external libraries unless necessary, just simple helpers for my own purposes), no live previews, steeper learning curve.
Now, you could imagine how I felt when I heard and saw what SwiftUI is capable of.
Hyped up, right after the keynote I immediately rushed to download Catalina and the new Xcode — my MacBook has suddenly become a battleground and I didn’t care too much about what would happen to it.
So I opened up the documentation and tutorials and started testing, testing and once more testing.
Different abominations of the layouts and lots of compilation errors.
But I finally got some understanding of how it works.
So I spent the following next days watching the next WWDC videos (that took some time, and no, not all of them), checking Twitter every minute for news from other developers, accustoming to the new ways of creating layouts, and waiting for Combine.
The Implementation and First ImpressionsSo an idea was born in my head — I decided to create a simple clone of the OLX app using SwiftUI and Combine.
And I started doing just that.
I found a boilerplate on GitHub which seemed to fit my requirements — basic extension for URLSession, and that’s pretty much the only thing I needed.
Having played around with SwiftUI a bit, I chose to advance with View Models for the backing logic, and the Repository pattern (example) acting as a bridge between the API Client and the View Models.
The repositories made use of a custom API Client I wrote and within them transformed the sequences to my desired outputs — for example, errors to empty arrays.
My first impression of Combine is that it is very similar to RxSwift (there’s even a cheat sheet from RxSwift to Combine already), and if you knew RxSwift — getting to know Combine would be very easy, and vice versa after learning Combine.
Overall — I really do recommend getting to know some kind of FRP if you haven’t already.
OffersListView with a composition of subviewsCombine seems to be a quite easy framework to use.
In comparison to RxSwift, it seems to have its advantages and disadvantages.
On one hand, you can assign the value of a KVO-compliant property from a publisher (there’s already a Merge Request of the same feature on the way for RxSwift — quick reaction!), on the other, chaining operators results in a very long result type which you need to .
eraseToAnyPublisher() to get a generic AnyPublisher<ResultType, Error> type — what RxSwift offers out of the box when dealing with Observables only.
In my feeling the naming of Combine’s operators is a bit better, for example, what is flatMapLatestin RxSwift, is switchToLatest in Combine — that makes more sense for me.
What I’m still missing is traits like Maybe, Completable (stares at Apple).
The ResultThe outcome of the OLX SwiftUI & Combine experimentThis is the outcome of what I have learned so far, and most importantly what is possible to do up to date in SwiftUI.
The example fetches a list of 40 offers from OLX and displays them in a list.
The images are lazy loaded upon appearing: each with a title, location, price, and a fixed date.
This entire project took me around 12 hours to code, with breaks, with analyzing, with calling for help.
While coding I tried preserving good quality, making sure the code is easy to read and everything else follows good practices.
Download this example project here.
It was stripped off of any real API calls, so no hacking for you, but the simple API client with Combine support is left in place, so you can do whatever you feel with it 🙂 You just need Xcode 11 beta to run it, no need for macOS Catalina.
Final ThoughtsSwiftUI is no doubt a revolution in how you create views.
It’s easy, it’s fast to create.
But it’s a beta.
And I while my heart is in love, my brain says it’s not yet time.
The demos, the first impression — it all makes you really excited, I’m a bit of an FRP fan so this fits right into my programming style.
Unfortunately, as of now, there are still too many limitations.
Too many puzzles are missing to make this the right choice for your production app; no wonder — it’s still a beta!.So, what is it that is missing?.For example:You can’t set navigation bar/tab bar colorsYou can’t set a TabbedView’s item title & icon at the same timeYou can’t build a SwiftUI project to Mac yetCombine is not yet fully integrated in FoundationThe Foundation integration for the Combine framework is unavailable.
The following Foundation and Grand Central Dispatch integrations with Combine are unavailable: KeyValueObserving, NotificationCenter, RunLoop, OperationQueue, Timer, URLSession, DispatchQueue, JSONEncoder, JSONDecoder, PropertyListEncoder, PropertyListDecoder, and the @Published property wrapper.
(51241500)There’s no way to watch Lists’ offset (no way to implement infinite scroll)No SwiftUI way for a collection view (apart from hosting UIKit)No way to change small details like separatorStyle in UITableViewI’m not sure there is a way to inject protocols as environment objects yetWe’ll get back to this in September at the Keynote… ;)If you want to get in touch, catch me on Twitter or by mail aleksander.