Codementor Events

How and why I built MVVM-C Swift architecture using Realm

Published Aug 13, 2018

About me

My name is Jay Balderas and have been doing iOS development for some time, web development coming second in my set of skills. I like to tackle new challenges everyday and thats what encouraged me to start my own software development firm as well.

The problem I wanted to solve

I was trying to figure out a good way to isolate certain logic portions of the app so that everything wouldn't get convoluted. This architecture ended being pretty awesome both in customizing and isolating tests. There were a lot of changes along the way and this make it that much easier to change and adapt with any feature that was added.

What is MVVM-C Swift architecture using Realm?

I built a social media application on the app store called "Pictorious". This application involves a lot of different collection views, collection views embedded in other table/collection views etc. etc. and having the right architecture made it mush easier to change and build when adding new features.

Tech stack

I felt that having something that was "reactive" was necessary for a social media app as it allowed users not to get "bored" and while waiting for things to load. For this reason is why I decided to use the Realm Object Server. I needed somewhere to host this server so I went with the ol'trusty AWS. Implementing video into an app can also have some challenges and can be a big strain when there is many users. For this reason I used S3 for image/video storage, Amazon cloudfront for video streaming, and Amazon lamba backend to convert those uploaded videos into streamable types that everyone can view.

The process of building MVVM-C Swift architecture using Realm

The process of building a home feed that had many moving parts gave space to a lot of errors. Everyone knows that when you build a feed like this one, there could be many "diffing" issues if there is either new or deleted content. If you were to load something on the app and 2 seconds later someone decided to delete their post, your table/collection view no longer "know" that something was deleted, so it would continue to try and load the cells that it thought was still there and you end up with a crash. Realm alleviated a lot of this. Having put a ViewModel that held the data and the ViewController that held the Viewmodel allowed you to pass that same ViewModel across different ViewControllers using coordinators.

Challenges I faced

Some challenges I found while building this application was the fact that the views and designs were always changing with this particular client. If I was someone still building in the MVC architecture, I would have been screwed as I would have to refactor everything inside that Viewcontroller. It wasn't until I found the concept of dynamic Viewmodels that things started looking a lot brighter. Getting rid of segues and even some screens in the storyboard allowed me to pass data along any popup, detail view, viewcontroller and even text messages.

Key learnings

If you have a big project, planning is always key. Knowing exactly what you use, the pros and cons to using a particular stack deep into detail will alleviate a lot of refactoring along the way. If Realm already gives me the reactive feel with their notifications, I also shouldn't use a library like rxSwift. Small things like this are something to think about.

Tips and advice

Get the feel for the MVVC architecture on a smaller scale. Getting objects to pass through each cell using protocols is essential in making everything run smoothly.

Final thoughts and next steps

I would like to make certain parts of the project more reactive and see how that goes with the user base. At the moment, I am no longer working on the project, but my upcoming ones will definitely have this architecture!

Discover and read more posts from Jay Balderas
get started
post commentsBe the first to share your opinion
Show more replies